Git--Create and merge branches

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, that is, the branch master . Rather than pointing to the commit, it is pointing HEAD master to the master commit, so HEAD pointing to the current branch.

In the beginning, the master branch is a line, git points to the master latest commit, and then HEAD points to master determine the current branch, and the commit point of the current branch:

Each commit, the master branch moves forward, so that as you continue to commit, master the lines of the branch are getting longer:

When we create a new branch, for example dev , Git creates a new pointer called dev , points to the master same commit, and then HEAD points dev to it, indicating that the current branch is dev on:

You see, Git creates a branch very quickly, because in addition to adding a dev pointer, changing HEAD the point, the workspace of the file does not have any changes!

However, from now on, the work area is modified and submitted to the dev branch, such as after the new submission , the dev pointer moves forward one step, and the master pointer does not change :

If our dev work is done, we can merge it into the dev master top. How does git merge? The simplest way to do this is to directly master point to dev the current commit 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 branches. Deleting a branch is to delete the dev dev pointer and delete it, leaving us with a master branch:

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

Let's start with combat.

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

$ git checkout-new'dev'

git checkoutThe command plus -b parameter represents creation and switching, equivalent to the following two commands:

' Dev '

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

$ git Branch* Dev  master

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

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

New  is quick.

Then submit:

" Branch Test "  11 insertion (+)

Now that the dev branch work is done, we can switch back to the master branch:

git checkout Master ' Master '

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

Now, let dev 's merge the work of the branch into the master branch:

git merge dev Updating D17efd8. Fec145afast-|    1 1 1 insertion (+)

git merge The 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 dev newest commits to the branch are exactly the same.

Taking note of the above Fast-forward information, Git tells us that this merge is "Fast forward mode", that is, direct master pointing to dev the current commit, so the merging speed is very fast.

Of course, not every time a merger is possible Fast-forward , we'll talk about other ways of merging later.

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

git branch-  d devDeleted branch Dev (was fec145a).

After deletion, look branch , only master the branches are left:

git branch* Master

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


GIT encourages the use of branching:

To view branches:git branch

To create a branch:git branch <name>

To switch branches:git checkout <name>

Create + switch Branches:git checkout -b <name>

Merge a branch to the current branch:git merge <name>

To delete a branch:git branch -d <name>

Git--Create and merge branches

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: 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.