Creating and merging branches-git Getting Started tutorial

Source: Internet
Author: User

In the version fallback, you already know that each time you commit, git strings them into a timeline, which is a branch. As of now, there is only one time line, in Git, this branch is called the main branch, the Master branch. The head is strictly not pointing to the commit, but pointing to Master,master is pointing to the commit, so the head is pointing to the current branch.

At first, the master branch is a line, and Git uses master to point to the latest commit, and then head to master to determine the current branch and the commit point of the current branch:

The Master branch moves forward one step at a time, so that as you continue to commit, the master branch line is getting longer:

When we create a new branch, such as Dev, Git creates a new pointer called Dev, points to the same commit as Master, and then points the head to dev, which indicates that the current branch is on Dev:

You see, Git creates a branch very quickly, because in addition to adding a dev pointer, changing the head point, the workspace file does not change anything!

However, from now on, the workspace modification and submission is for the Dev branch, such as after the new commit, the dev pointer moves forward one step, and the master pointer does not change:

If our work on Dev is done, we can merge dev into master. How does git merge? The simplest way to do this is to direct master to the current commit of dev and complete the merge:

So git merge branch is fast too! Just change the pointer, the work area content is also unchanged!

After you merge the branches, you can even delete the dev branch. Removing the dev branch is to delete the dev pointer and delete it, we have a master branch left:

It's amazing, you can see that some commits are done by branch?

The following starts the actual combat.

First, we create the dev branch and then switch to the dev branch:

$ git checkout -b devSwitched to a new branch ‘dev‘

The git checkout command plus the-b parameter means create and switch, equivalent to the following two commands:

$ git branch dev$ git checkout devSwitched to branch ‘dev‘

Then, use the GIT branch command to view the current branch:

$ git branch* dev  master

The git branch command lists all branches, preceded by an * number in front of the current branch.

Then we can commit normally on the dev branch, such as making a change to Readme.txt, plus a line:

Creating a new branch is quick.

Then submit:

$ git add readme.txt $ git commit -m "branch test"[dev fec145a] branch test 1 file changed, 1 insertion(+)

Now that the Dev branch's work is done, we can switch back to the master branch:

$ git checkout masterSwitched to branch ‘master‘

After switching back to the master branch, and then looking at a Readme.txt file, the content just added is gone! Because that commit is on the dev branch, the commit point of the master Branch at the moment does not change:

Now, let's merge the work of the Dev branch onto the master branch:

$ git merge devUpdating d17efd8..fec145aFast-forward readme.txt |    1 + 1 file changed, 1 insertion(+)

The git merge command is used to merge the specified branch into the current branch. After merging, and then looking at the contents of the Readme.txt, you can see that the latest commit with the Dev branch is exactly the same.

Taking note of the fast-forward information above, Git tells us that this merger is a "fast-forward mode", which is to direct master to the current commit of Dev, so the merging is very fast.

Of course, not every merger can be fast-forward, and we'll merge other ways later.

Once the merge is complete, you can safely delete the dev branch:

$ git branch -d devDeleted branch dev (was fec145a).

After deleting, view branch, only the master branch is left:

$ git branch* master

Because creating, merging, and deleting branches is fast, git encourages you to use branches to accomplish a task, merge and then delete the branch, which works the same as directly on the master branch, but it is more secure.

Summary

GIT encourages the use of branching:

View branches: Git branch

Create a branch: Git branch name

Switch branches: git checkout name

Create + Switch branch: git checkout-b name

Merge a branch to the current branch: git merge name

Delete branch: git branch-d name

Creating and merging branches-git Getting Started tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.