Chapter 4 git branch operations

,
Picture viewed from left to right

My own understanding: Under normal circumstances, developers can develop in multiple branches, and finally merge it into the main branch. Testers test operation and maintenance and deploy it to the server. The server code needs to be upgraded to establish a branch.

4.1 What is a branch?

In the version control process, to advance multiple tasks simultaneously, for each task, we can create a separate
branch for each task. Using branches means that programmers can separate their work from the main line of development. When developing their own branches
, it will not affect the operation of the main line branches. For beginners, branches can be simply understood as copies, and a branch is
a separate copy. (The bottom layer of the branch is actually a reference to the pointer)

简单来说就是在使用版本控制工具开发的过程中,同时推进多个任务(并行互不影响)

Insert image description here

4.2 Benefits of branching

  1. Promote the development of multiple functions in parallel at the same time to improve development efficiency
  2. During the development process of each branch, if the development of one branch fails, it will not have any impact on other branches. Just delete the failed branch and start again.

4.3 Branch operation commands

branch name effect
git branch branch name Create a branch
git branch -v View branches
git checkout branch name switch branch
git merge branch name Merge the specified branch into the current branch
git branch -D branch name delete branch

4.3.1 Branch operations

①Create branches and switch branches

The * indicates that the current branch is master.

Insert image description here

② Make different modifications in two different branches.

When merging branches, two branches must be involved. One of these two branches is the "current branch" and the other is the "target branch".
Command writing: git merge target branch
So the essence of the branch merge command is to merge the "target branch" into the "current branch".

例如:把hotfix合并到master git merge hotfix 需要确保当前所在的分支是master

例如:把master合并到hotfix git merge master 需要确保当前所在的分支是hotfix

Merge the current version of hot-fix into the master branch
Insert image description here

②Merge branch conflicts

Cause of conflict:
When merging branches, if there are two completely different sets of modifications to the same file in different branches at the same location, a conflict will occur.
git can't help us decide which one to use. The content of the new code must be decided manually.
Insert image description here

The specific content of the conflict

Insert image description here

(1) Conflict resolution
  1. Delete the special symbols and modify them to what you need.
    Special symbols
<<<<< HEAD 
内容冲突1
=======
内容冲突2
>>>>>>hot-fix
  1. Check the status (it is detected that a file has two modifications) MERGING indicates a conflict
    Insert image description here
  2. Add a temporary storage area to submit the local library
    . Note: You cannot use the git commit command with a file name or an error will be reported.
    Insert image description here

4.3.2 The underlying implementation of branches

Insert image description here
Master and hot-fix are actually pointers to specific version records. The current branch is actually determined by HEAD,
so the essence of creating a branch is to create one more pointer. "
If HEAD points to master, then we are now on the master branch.
If HEAD points to hot-fix, then we are now on the hot-fix branch.
So the essence of switching branches is to move the HEAD pointer.

You can also check which branch HEAD points to through the following file.
The current path is in the .git folder in the local library.
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/wyr1235/article/details/128826197
Recommended