Git branch management



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 branchgit branch <name>
  • Switch branchgit checkout <name>
  • Create and switch branchesgit checkout -b <name>
  • Merge the specified branch to the current branch,git merge <name>
  • Delete branchgit 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.emasterBranch,

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 usesmasterPoint to the latest submission, and thenHEADpointmaster, you can determine the current branch and the commit point of the current branch.

Git branch management

Each submission,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 asdevBranch, GIT creates a new pointer calleddev, pointingmasterThe same submission, and thenHEADpointdev, which means that the current branch isdevCome on.

Git branch management

Git creates branches quickly because git only adds onedevPointer and modify at the same timeHEADThere is no change in the file pointing to the workspace.

Modifications and submissions to the workspace are now targeted atdevBranches, such as after a new submission,devThe pointer moves forward andmasterPointer unchanged

Git branch management

When we aredevWhen the work on the is finished, you candevmerge tomasterCome on.

Git’s merger is very simple, that is, directlymasterpointdevIs currently committed, which completes the merge.

Git branch management

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

Git branch management

Create a branch.

  • establishdevBranch and switch todevbranch
$ 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 stepdev2。 as follows
$ git branch dev2
$ git checkout dev2
Switch to branch 'dev2'
  • git branchView current branch
$ git branch
* dev2

git branchList all branches, with a * sign in front of the current branch.

  • You can now commit normally on the dev2 branch. If modifiedreadme.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 indev2Up and in the workspacereadme.txtmodify

  • 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 branchdev2Submission completed on.

  • staydev2After submitting on the branch, switch backmasterBranch:
$ 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 todev2Branch, you can seedev2The 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

  • takedev2Merge branch content intomasterBranch (current branch is)master
$ git merge dev2
Update 036ced2 03d07d2
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

View nowreadme.txtContent, already anddev2equally

git mergeCommand is used to merge a specified branch into the current branch.

Note that the above prompts when mergingFast-forward, GIT indicates that the merge is “fast forward mode”, that is, putmasterpointdev2Is currently submitted, so it’s fast.

Delete branch

  • deletedev2Branches can be deleted after mergingdev2It’s branching.
$ git branch -d dev2
Branch dev2 (formerly 03d07d2) was deleted.
$ git branch
* master

After deletion, viewbranchdev2The 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.