git merge merge branches

 

git merge is used for branch merging, merging the contents of other branches into the current branch. For example, the branch structure is as follows:

                        master
                         /
C0 ---- C1 ---- C2 ---- C4
                         \
                         C3 ---- C5
                                  \
                                issueFix

The current branch is master
$ git checkout master

Merge the content in issueFix:
$ git merge issueFix

If there are no conflicts, the merge completes. If there is a conflict, git will prompt that there is a conflict in that file, such as the following conflict:

<<<<<<< HEAD:test.c

printf (“test1″);

=======

printf (“test2″);

>>>>>>> issueFix:test.c

You can see that the upper part separated by ======= is the content in HEAD (that is, the master branch, the branch checked out when running the merge command), and the lower part is the content in the issueFix branch. The solution to conflict is to choose one of the two or to integrate it yourself. For example, you can solve this by replacing this content with the following:

printf (“test2″);

这个解决方案各采纳了两个分支中的一部分内容,而且删除了 <<<<<<<,=======,和>>>>>>> 这些行。在解决了所有文件里的所有冲突后,运行 git add 将把它们标记为已解决(resolved)。因为一旦暂存,就表示冲突已经解决。如果你想用一个有图形界面的工具来解决这些问题,不妨运行 git mergetool,它会调用一个可视化的合并工具并引导你解决所有冲突:

$ git mergetool
merge tool candidates: kdiff3 tkdiff xxdiff meld gvimdiff opendiff emerge vimdiff
Merging the files: index.html

Normal merge conflict for ‘test.c’:
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (kdiff3):

合并后的分支图如下:

                               master
                                 /
C0 ---- C1 ---- C2 ---- C4 ---- C6
                        \       /
                        C3 ----C5
                                \
                              issueFix

注意,这次合并的实现,由于当前 master 分支所指向的 commit (C4)并非想要并入分支(issueFix)的直接祖先,Git 不得不进行一些处理。就此例而言,Git 会用两个分支的末端(C4 和 C5)和它们的共同祖先(C2)进行一次简单的三方合并。对三方合并的结果作一新的快照,并自动创建一个指向它的 commit(C6)

退出合并工具以后,Git 会询问你合并是否成功。如果回答是,它会为你把相关文件暂存起来,以表明状态为已解决。然后可以用 git commit 来完成这次合并提交。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327065388&siteId=291194637