Branch Introduction
Multiple branches are lines of records that have multiple commits in the repository, as shown in the blue Master is a branch, the Red Dev is also a branch, and the head refers to the current branch:
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.
Create a branch
Previously we learned that when building a git repository, a master branch is automatically generated, the main branch , and head 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.
Now let's create a new branch dev, using the command git checkout -b dev
, which indicates that branch Dev was created and switched to the Dev Branch, equivalent to the following two commands git branch dev
and git checkout dev
You can then use git branch
it to view the branch, as indicated by the current branch:
Now the branching situation is like this:
Merging branches
Now our current branch is Dev, now make a change to README.txt, add a line create a branch named dev
and commit
Now that the Dev branch is done, git checkout master
switch back to the master branch, switch back to the master branch, and then look at a README.txt file, and the content you 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, we merge the work of the Dev branch onto the master branch, using git merge dev
the command to merge the specified branch dev to the current branch master, and then view README.txt to see the latest commit of the dev branch:
Now the branching situation is like this:
In effect, the merge is done by pointing master directly to the current commit of Dev.
Delete Branch
After you merge the branches, you can even delete the dev branch. Using the command git branch -d dev
, removing the dev branch is to delete the dev pointer and delete it, we have a master branch left:
Structure diagram:
GIT Operations-branch management (i)