This article is reproduced and referred to teacher Liao Xuefeng’s blogGit tutorial。 Learn and record according to his blog. Thank him for his selfless sharing. You are also welcome to check the original text.
- Branches can manage versions in parallel, write code together, complete work on branches, and then merge branches.
- View branch and current branch:
- Create branch
git branch <name>
- Switch branch
git checkout <name>
- Create and switch branches
git checkout -b <name>
- Merge the specified branch to the current branch,
git merge <name>
- Delete branch
git branch -d <name>
Branches are similar to multiple branches in parallel.
Function of branch: for example, in multi person collaborative development, the functions you develop need to be completed in two weeks, and the first week has not been completed. If you submit, because the code is incomplete, others may not be able to work. However, if you submit after all the codes are written, there is a risk of loss.
With the help of branches, you can create your own branch. Multiple people work normally on their own branches without affecting each other. After the development is completed, they are merged into the original branch at one time to ensure safety, efficiency, coordination and complementary influence.
Git branch creation, switching and deletion are very fast, regardless of the number of files.
Create and merge branches
In version fallback, GIT’s version management is to string each submission into a time, and this timeline is a branch.
The GIT warehouse currently created has only one branch – the main branch, i.e
Strictly speaking, head does not point to submission, but to master, which points to submission. Therefore, head points to the current branch.
master A branch is a line that git uses
masterPoint to the latest submission, and then
master, you can determine the current branch and the commit point of the current branch.
masterThe branch will move forward. With the continuous submission, the line of the master branch will become longer and longer.
When we create new branches, such as
devBranch, GIT creates a new pointer called
masterThe same submission, and then
dev, which means that the current branch is
Git creates branches quickly because git only adds one
devPointer and modify at the same time
HEADThere is no change in the file pointing to the workspace.
Modifications and submissions to the workspace are now targeted at
devBranches, such as after a new submission,
devThe pointer moves forward and
When we are
devWhen the work on the is finished, you can
Git’s merger is very simple, that is, directly
devIs currently committed, which completes the merge.
Git merges branches quickly, just changing the pointer and the content of the workspace remains unchanged.
After merging, if you delete the dev branch, just delete the dev pointer
There is only one master branch left
Create a branch.
devBranch and switch to
$ git checkout -b dev Switch to a new branch 'dev'
git checkout -b xxRepresents creating a branch and switching.
- Create and switch branches step by step
dev2。 as follows
$ git branch dev2 $ git checkout dev2 Switch to branch 'dev2'
git branchView current branch
$ git branch dev * dev2 master
git branchList all branches, with a * sign in front of the current branch.
- You can now commit normally on the dev2 branch. If modified
readme.txtFile. Then view the GIT status
$ git status At branch dev2 Changes that have not been staged for submission: (use "git add < File >..." Update content to submit) (use "git checkout -- < File >..." Discard workspace changes) Modify: readme txt Modify the submission that has not been added (use "git add" and / or "git commit - a")
Prompt that the current branch is in
dev2Up and in the workspace
- Branch commit
$ git add readme.txt $ git commit -m"modify readme.txt at branch" [dev2 03d07d2] modify readme.txt at branch 1 file changed, 1 insertion(+)
As above, in the current branch
dev2Submission completed on.
dev2After submitting on the branch, switch back
$ git checkout master Switch to branch 'master' Your branch is consistent with the upstream branch 'origin / Master'. [email protected]:~/gitTest$ cat readme.txt `this is a test that I learn and use git version control system this is a beginning wofaidognyixie dognxi
As you can see,
readme.txtThe document shall be consistent with that before modification.
- Switch to
dev2Branch, you can see
dev2The content on the branch is the latest modified content
$ git checkout dev2 Switch to branch 'dev2' $ cat readme.txt `this is a test that I learn and use git version control system this is a beginning wofaidognyixie dognxi create two new branch
dev2Merge branch content into
masterBranch (current branch is)
$ git merge dev2 Update 036ced2 03d07d2 Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+)
readme.txtContent, already and
git mergeCommand is used to merge a specified branch into the current branch.
Note that the above prompts when merging
Fast-forward, GIT indicates that the merge is “fast forward mode”, that is, put
dev2Is currently submitted, so it’s fast.
dev2Branches can be deleted after merging
$ git branch -d dev2 Branch dev2 (formerly 03d07d2) was deleted. $ git branch dev * master
After deletion, view
dev2The branch is gone.
Git creates, merges and deletes branches very quickly, so git encourages using branches to complete a task, and then deletes branches after merging, which is efficient and safe.