remarks:
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.
Knowledge points
- Branches can manage versions in parallel, write code together, complete work on branches, and then merge branches.
- View branch and current branch:
git 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>
Branch Management
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.emaster
Branch,
Strictly speaking, head does not point to submission, but to master, which points to submission. Therefore, head points to the current branch.
Start,master
A branch is a line that git usesmaster
Point to the latest submission, and thenHEAD
pointmaster
, you can determine the current branch and the commit point of the current branch.
Each submission,master
The 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 asdev
Branch, GIT creates a new pointer calleddev
, pointingmaster
The same submission, and thenHEAD
pointdev
, which means that the current branch isdev
Come on.
Git creates branches quickly because git only adds onedev
Pointer and modify at the same timeHEAD
There is no change in the file pointing to the workspace.
Modifications and submissions to the workspace are now targeted atdev
Branches, such as after a new submission,dev
The pointer moves forward andmaster
Pointer unchanged
When we aredev
When the work on the is finished, you candev
merge tomaster
Come on.
Git’s merger is very simple, that is, directlymaster
pointdev
Is 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.
- establish
dev
Branch and switch todev
branch
$ git checkout -b dev
Switch to a new branch 'dev'
git checkout -b xx
Represents 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 branch
View current branch
$ git branch
dev
* dev2
master
git branch
List all branches, with a * sign in front of the current branch.
- You can now commit normally on the dev2 branch. If modified
readme.txt
File. 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 indev2
Up and in the workspacereadme.txt
modify
- 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 branchdev2
Submission completed on.
- stay
dev2
After submitting on the branch, switch backmaster
Branch:
$ 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.txt
The document shall be consistent with that before modification.
- Switch to
dev2
Branch, you can seedev2
The 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
Merge branch
- take
dev2
Merge branch content intomaster
Branch (current branch is)master
)
$ git merge dev2
Update 036ced2 03d07d2
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
View nowreadme.txt
Content, already anddev2
equally
git merge
Command 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, putmaster
pointdev2
Is currently submitted, so it’s fast.
Delete branch
- delete
dev2
Branches can be deleted after mergingdev2
It’s branching.
$ git branch -d dev2
Branch dev2 (formerly 03d07d2) was deleted.
$ git branch
dev
* master
After deletion, viewbranch
,dev2
The 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.