分支与合并

<div class="reader-container" style="width: 808px; height: 608px; margin: 0px auto; color: rgb(85, 81, 71); font-family: 'Microsoft Yahei'; font-size: 18px; line-height: 32.4px; background-color: rgb(251, 250, 248);"><div class="reader-content"><div class="reader-article-container" style="padding: 18px 50px; border-top-width: 0px;"><div class="reader-article" style="overflow: hidden;"><div class="reader-header" style="margin: 40px 0px;"><h1 class="reader-header-title" style="font-size: 27px; margin: 0.67em 0px;"><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">原文:  </p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">http://gitbook.liuhui998.com/3_3.html</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">http://gitbook.liuhui998.com/5_3.html</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><strong>一、如何分支的合并</strong></p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">在git中,可以使用git merge 和git rebase两个命令来进行分支的合并。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">git merge 和git rebase在大体上都差不多,下文主要以git merge来例来讲解分支的合并流程。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">如果你想了解分支合并的更多内容,请阅读《git merge简介</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">》,《git rebase简介(基本篇)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">》和《git rebase简介(高级篇)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">》。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">git merge命令示例:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git merge branchname</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">这个命令把分支"branchname"合并到了当前分支里面。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">如有冲突(冲突--同一个文件在远程分支和本地分支里按不同的方式被修改了);那么命令的执行输出就像下面一样</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git merge next</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"> 100% (4/4) done</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">Auto-merged file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">CONFLICT (content): Merge conflict in file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">Automatic merge failed; fix conflicts and then commit the result.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">在有问题的文件上会有冲突标记,在你手动解决完冲突后就可以把此文件添 加到索引(index)中去,用git commit命令来提交,就像平时修改了一个文件 一样。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">如果你用gitk来查看commit的结果,你会看到它有两个父分支:一个指向当前的分支,另外一个指向刚才合并进来的分支。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><strong>二、解决合并中的冲突</strong></p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">如果执行自动合并没有成功的话,git会在索引和工作树里设置一个特殊的状态, 提示你如何解决合并中出现的冲突。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">有冲突(conflicts)的文件会保存在索引中,除非你解决了问题了并且更新了索引,否则执行 git commit都会失败:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git commit</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">file.txt: needs merge</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">如果执行 git status 会显示这些文件没有合并(unmerged),这些有冲突的文件里面会添加像下面的冲突标识符:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><<<<<<< HEAD:file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">Hello world</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">=======</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">Goodbye</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">你所需要的做是就是编辑解决冲突,(接着把冲突标识符删掉),再执行下面的命令:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git add file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git commit</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">注意:提交注释里已经有一些关于合并的信息了,通常是用这些默认信息,但是你可以添加一些你想要的注释。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">上面这些就是你要做一个简单合并所要知道的,但是git提供更多的一些信息来 帮助解决冲突。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><strong>三、撒销一个合并</strong></p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">如果你觉得你合并后的状态是一团乱麻,想把当前的修改都放弃,你可以用下面的命令回到合并之前的状态:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git reset --hard HEAD</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">或者你已经把合并后的代码提交,但还是想把它们撒销:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git reset --hard ORIG_HEAD</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">但是刚才这条命令在某些情况会很危险,如果你把一个已经被另一个分支合并的分支给删了,那么 以后在合并相关的分支时会出错。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">关于撤销的更多内容请参考《git reset简介</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">》</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><strong>四、快速向前合并</strong></p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">还有一种需要特殊对待的情况,在前面没有提到。通常,一个合并会产生一个合并提交(commit), 把两个父分支里的每一行内容都合并进来。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">但是,如果当前的分支和另一个分支没有内容上的差异,就是说当前分支的每一个提交(commit)都已经存在另一个分支里了,git 就会执行一个“快速向前"(fast forward)操作;git 不创建任何新的提交(commit),只是将当前分支指向合并进来的分支。</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><strong>五、在合并过程中得到解决冲突的协助</strong></p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">git会把所有可以自动合并的修改加入到索引中去, 所以git diff只会显示有冲突的部分. 它使用了一种不常见的语法:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">diff --cc file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">index 802992c,2b60207..0000000</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">--- a/file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">+++ b/file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">@@@ -1,1 -1,1 +1,5 @@@</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">++<<<<<<< HEAD:file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"> +Hello world</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">++=======</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">+ Goodbye</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">++>>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">回忆一下, 在我们解决冲突之后, 得到的提交会有两个而不是一个父提交: 一个父提交是当前分支提交前的HEAD,; 另外一个父提交是被合并分支的HEAD, 被暂时存在MERGE_HEAD.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">在合并过程中, 索引中保存着每个文件的三个版本. 三个"文件暂存(file stage)"中的每一个都代表了文件的不同版本:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git show :1:file.txt  # 两个分支共同祖先中的版本.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git show :2:file.txt  # HEAD中的版本.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git show :3:file.txt  # MERGE_HEAD中的版本.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">当你使用git diff去显示冲突时, 它在工作树(work tree), 暂存2(stage 2)和暂存3(stage 3)之间执行三路diff操作, 只显示那些两方都有的块(换句话说, 当一个块的合并结果只从暂存2中得到时, 是不会被显示出来的; 对于暂存3来说也是一样).</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">上面的diff结果显示了file.txt在工作树, 暂存2和暂存3中的差异. git不在每行前面加上单个'+'或者'-', 相反地, 它使用两栏去显示差异: 第一栏用于显示第一个父提交与工作目录文件拷贝的差异, 第二栏用于显示第二个父提交与工作文件拷贝的差异. (参见git diff-files中的"COMBINED DIFF FORMAT"取得此格式详细信息.)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">在用直观的方法解决冲突之后(但是在更新索引之前), diff输出会变成下面的样子:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">diff --cc file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">index 802992c,2b60207..0000000</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">--- a/file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">+++ b/file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">@@@ -1,1 -1,1 +1,1 @@@</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">- Hello world</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">-Goodbye</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">++Goodbye world</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">上面的输出显示了解决冲突后的版本删除了第一个父版本提供的"Hello world"和第二个父版本提供的"Goodbye", 然后加入了两个父版本中都没有的"Goodbye world".</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">一些特别diff选项允许你对比工作目录和三个暂存中任何一个的差异:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff -1 file.txt      # 与暂存1进行比较</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff --base file.txt          # 与上相同</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff -2 file.txt      # 与暂存2进行比较</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff --ours file.txt          # 与上相同</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff -3 file.txt      # 与暂存3进行比较</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git diff --theirs file.txt    # 与上相同.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">还有,git log和gitk命令也为合并操作提供了特别的协助:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git log --merge</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ gitk --merge</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">这会显示所有那些只在HEAD或者只在MERGE_HEAD中存在的提交, 还有那些更新(touch)了未合并文件的提交.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">你也可以使用git mergetool, 它允许你使用外部工具如emacs或kdiff3去合并文件.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">每次你解决冲突之后, 应该更新索引:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git add file.txt</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">完成索引更新之后, git-diff(缺省地)不再显示那个文件的差异, 所以那个文件的不同暂存版本会被"折叠"起来.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><strong>六、多路合并</strong></p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">你可以一次合并多个头, 只需简单地把它们作为git merge的参数列出. 例如,</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git merge scott/master rick/master tom/master</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">相当于:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git merge scott/master</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git merge rick/master</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git merge tom/master</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;"><strong>七、子树</strong></p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">有时会出现你想在自己项目中引入其他独立开发项目的内容的情况. 在没有路径冲突的前提下, 你只需要简单地从其他项目拉取内容即可.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">如果有冲突的文件, 那么就会出现问题. 可能的例子包括Makefile和其他一些标准文件名. 你可以选择合并这些冲突的文件, 但是更多的情况是你不愿意把它们合并. 一个更好解决方案是把外部项目作为一个子目录进行合并. 这种情况不被递归合并策略所支持, 所以简单的拉取是无用的.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">在这种情况下, 你需要的是子树合并策略.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">这下面例子中, 我们设定你有一个仓库位于/path/to/B (如果你需要的话, 也可以是一个URL). 你想要合并那个仓库的master分支到你当前仓库的dir-B子目录下.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">下面就是你所需要的命令序列:</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git remote add -f Bproject /path/to/B (1)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git merge -s ours --no-commit Bproject/master (2)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git read-tree --prefix=dir-B/ -u Bproject/master (3)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git commit -m "Merge B project as our subdirectory" (4)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">$ git pull -s subtree Bproject master (5)</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">子树合并的好处就是它并没有给你仓库的用户增加太多的管理负担. 它兼容于较老(版本号小于1.5.2)的客户端, 克隆完成之后马上可以得到代码.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">然而, 如果你使用子模块(submodule), 你可以选择不传输这些子模块对象. 这可能在子树合并过程中造成问题.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">译者注: submodule是Git的另一种将别的仓库嵌入到本地仓库方法.</p><p style="color: rgb(51, 51, 51); margin-top: 0px; margin-bottom: 22px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft Yahei', Simsun; font-size: 17px; line-height: 28px;">另外, 若你需要修改内嵌外部项目的内容, 使用子模块方式可以更容易地提交你的修改.</p>
</h1></div></div></div></div></div>
posted @ 2015-11-29 16:32  窗外临街  阅读(183)  评论(0编辑  收藏  举报