Understanding Git--branch (Branch)

Source: Internet
Author: User
Tags commit diff

1, the purpose of the branch

Suppose you're writing an essay. You have written the first version and submitted a reviewer. Later, you get new data, and you're adding your data to the paper. In the process, the reviewer lets you change the format of your paper. Obviously, you cannot submit the version you are changing to them after you have modified the format. You only want to send them after the format has been modified on the basis of previous versions.

This is the idea behind the branch, and git makes it very easy.

Note The term: "Branch" and "head" are almost synonymous in git, each branch corresponds to a head, and each head represents a branch. Sometimes "branch" is used to refer to a head and all commits before the head, while "head" is used to refer to the last commit.

2. Create a branch

To create a branch, such as your warehouse:

(A)-(B)-(C)

|

Master

|

HEAD

To jump back to commit (b) and start a new job there, you should first know how to refer to this commit, you can use git log to get the SHA1 name of commit (b), or you use head^ to get him.

Now, let's use the git branch command:

Git branch [new-head-name] [reference-to-(B)]

Or, for example:

Git branch fix-headers head^

This command creates a new head with the specified name, and the head points to the specified commit object. If the Commit object is omitted, it points to the head.

Now, our commit tree looks like this:

(A)-(B)----------(C)

| |

Fix-headers Master

|

HEAD

3. Switch between different branches

To start a new job on a head, you should set Fix-headers to the current head, which is implemented by the git Checkout command:

git checkout [head-name]

This command does the following things:

(1) Make head point to head that is pointed to by [Head-name].

(2) Rewrite all files under the directory.

Note: If there are no commit changes when switching branches, git behaves strangely. So when switching branches, be sure to commit all the changes.

After switching to Fix-headers head, you made a change and commit, and the last repository might look like this:

+--------------------(D)

| |

(A)-(B)-(C) |

| |

Master Fix-headers

|

HEAD

You should now understand what is called a branch. The commit tree merits a branch.

4. Related commands

(1) Git branch: No parameters will list the existing heads, the current head is preceded by *

(2) Git diff [Head1]. [Head2]: Lists the differences between HEAD1 and head2

(3) Git diff [Head1] ... [Head2] (three points in the middle): Lists the differences between HEAD1 and head2 and all their ancestors.

(4) Git log [head1]. [Head2]: Lists the change logs between the common ancestors of head2 and Head1 and head2.

5, the usual branch usage mode

The Master branch is always in a redistributable state, and the other branches contain unfinished work, new features, and so on.

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.