Branching is the parallel universe in sci-fi movies, and while you're working on git in front of your computer, another one is working on SVN in another parallel universe.
If two parallel universes don't interfere with each other, that's not going to affect you now. However, at some point in time, two parallel universes merged, and as a result, you learned both Git and svn!.
What is the use of branching in practice? Suppose you are ready to develop a new feature, but it takes two weeks to complete, the first week you write 50% of the code, if submitted immediately, because the code is not finished, the incomplete code base will cause others can not work. There is a huge risk of losing your daily progress if you wait until the code is all written and then submitted again.
Now that you have a branch, don't be afraid. You create a branch of your own, others do not see, but also continue to work on the original branch, and you work on your own branch, you want to submit the submission, until the development is complete, then once merged into the original branch, so that it is safe, and does not affect the work of others.
Other version control systems such as SVN have branch management, but after use you will find that these version control system creation and switching branches than the snail is still slow, it is unbearable, the results branch function became a device, we do not use.
But git's branches are different, and git can do it in 1 seconds, whether it's creating, switching, or deleting branches! Whether your repository is 1 files or 10,000 files.
Create and Merge branches
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?
The following starts the actual combat.
First, we create the dev
branch and then switch to the dev
branch:
$ git checkout-'dev'
git checkout
The 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 branch
The 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:
Create New branch Dev.
Then submit:
" Create New branch .... " 1file1 insertion (+)
Now that the dev
branch work is done, we can switch back to the master
branch:
' Master ' Your Branchis up-to-date'origin/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 devupdating 90bc1f7. 45ae9a9fast-11file1 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 45ae9a9).
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.
Summary
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>
Resolve Conflicts
Life is not as good as ten, and merging branches is often not smooth sailing.
Prepare a new feature1
branch and continue our new branch development:
$ git checkout-'feature1'
Modify the last line of Readme.txt to read:
Create New branch Feature1.
Commit on the feature1
branch:
" Create new branch Feature1 first modify " 1file1 insertion (+)
Switch to master
branch:
' Master ' 'origin/master'1 commit. " git push " to publish your local commits)
Git also automatically prompts US master
that the current branch is ahead of 1 commits than a remote master
branch.
On the master
branch, change the last line of the Readme.txt file to:
GoBack master ....
Submit:
" GoBack Master First modify " 1file1 insertion (+)
Now, the master
branches and feature1
branches each have new commits, each of which becomes:
In this case, git cannot perform a "quick merge" and can only attempt to merge its own changes, but the merge may conflict, so let's try this:
$ git merge feature1auto- in then commit the result.
It's a conflict! Git tells us that there is a conflict with the Readme.txt file and must be resolved manually before committing. git status
You can also tell us the conflicting files:
$ git Statuson Branch Masteryour branch is ahead of'Origin/master'By2commits. ( use"git push"To publish your local commits) You have unmerged paths. (fix conflicts and run"git commit") unmerged paths: ( use"git add <file>, ... ."To Mark resolution) both MODIFIED:README.TXTNO changes added to commit ( use"git add"and/or"git commit-a")
We can view the contents of Readme.txt directly:
test git Modify secondstudy gitthree addfour Add modifyfive Add modifysix Add modifyseven add modifyeight add Modify. .. Create new branch Dev . <<<<<<< headgoback master .... =======Create New branch Feature1. >>>>>>> Feature1
Git uses <<<<<<<
, =======
to >>>>>>>
mark out the contents of different branches, we modify the following to save:
Test git modify secondstudy gitthree addfour Add modifyfive Add modifysix Add modifyseven add modifyeight add Modify ... cr Eate New Branch Dev. Create New branch Feature1. GoBack master ....
Re-submit:
" Fixed Conflicts " [Master 0f3d64a] Fixed conflicts
Now, the master
branches and feature1
branches become the following:
git log
you can also see the merging of branches with parameters:
$ git log--graph--pretty=oneline--abbrev-Commit*0f3d64a Fixed Conflicts|| *b4309b0 Create new branch Feature1 first modify* |0b56936 GoBack Master First modify|/*45ae9a9 Create New branch ....*90BC1F7 Test Name*c1bdf43 Test Commit*dd34c9a No add but Commit,because use other parameter*4ed30d1 Eight Modify dify*B45ca96 Eight Modify*9332D40 Seven Modify*72c6f9b Six Modify*F64b5a0 Five Modify*de8fd65 Four Modify*83a4b1e Three Modify*01C05CF Modify*1acafa7 First Modify* 09c1bba First Git
Finally, delete the feature1
branch:
$ git branch-d feature1deleted Branch Feature1 (was b4309b0).
Summary
When git cannot merge branches automatically, you must resolve conflicts first. After resolving the conflict, submit again, and the merge is complete.
git log --graph
you can see the branch merge diagram with the command.
GIT Branch Management: Create and Merge branches, resolve merge conflicts