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.