Creating and merging Git branches-branch operations
Documentation: Git branches - creating and merging branches
Create a branch and switch
At this time there is a requirement to work on the new branch iss53:
$ git checkout -b iss53 # b表示branchIt is the abbreviation of the following two commands:
$ git branch iss53
$ git checkout iss53switch branch
Suddenly there is an urgent problem to solve, which needs to be fixed in the original master branch:
$ git checkout masterBefore switching to master, the iss53 branch needs to be kept in a clean state (all modifications have been committed).
Note: Switching branches to Git will reset your working directory.
checkoutChinese meaning "check out",checkout <branch>Check out the branch => Check out the code of the specified branch => Reset the working directory and switch branches.
Next, you want to fix the urgent problem.Create a hotfix branch and work on it until the problem is solved:
$ git checkout -b hotfix
# 中间过程在hotfix上修改了代码并提交
$ echo 'test' > ./hotfix.txt
$ git add .
$ git commit -m 'fixed'merge branches
$ git checkout master # 首先切回master分支
$ git merge hotfix # 把 hotfix 分支合并过来delete branch
$ git branch -d hotfix # d表示delete
# 然后切回iss53继续工作
$ git checkout iss53Note that deleting branches is done on the branch command
Merge branches after multiple commits
Assume you have fixed issue #53 and plan to merge it into the master branch:
$ git checkout master
$ git merga iss53This does not seem to be much different from the previous merger.At this point your development history has diverged from an earlier point.Because, master The commit where the branch is located is not iss53 The direct ancestor of the commit on which the branch is located, Git has to do some extra work.When this happens, Git will use the snapshots pointed to by the ends of the two branches and the common ancestor of the two branches to make a simple Three-way merger 。
Different from pushing the branch pointer forward before, Git takes a new snapshot of the result of this three-party merge and automatically creates a new commit pointing to it.This is called a merge commit, and what's special about it is that it has more than one parent commit.
Branch merging when conflict occurs
If you are in two different branches, Different modifications were made to the same part of the same file , Git cannot merge them cleanly, and a conflict occurs.
CONFLICT promotion appears during the merge process, indicating a conflict
$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.Use git status to view the unmerged status.
Any files that are pending resolution because they contain merge conflicts will be identified as unmerged.Git will add standard conflict resolution markers to conflicting files, so you can open these conflicting files and resolve the conflicts manually. The conflicting file will contain some special sections, which will look like the following:
<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> iss53:index.htmlyou need Manually resolve conflicts , after resolving conflicts in all files, for each file Use git add command to mark it as conflict resolved. Once these conflicting files are staged, Git marks them as conflict resolved 。
If you are satisfied with the results and are sure that all conflicting files have been staged, you can enter git commit to complete the merge submission.