4.1.3. 手工合并
Pull Request提供的自动合并显示在提交日志中是什么样子的呢?以用户ossxp-com身份检出版本库,会看到用户wangsheng的提交已经合并到版本库中。
$ git clone git@github.com:gotgit/gotgit.git$ cd gotgit$ git log –graph -3* commit 6c1f1ee152629fd2f8d00ebe92c27a32d068d756|\ Merge: 00c6c4b 7ecdfe7| | Author: OpenSourceXpress <worldhello.net@gmail.com>| | Date: Tue Aug 16 01:23:47 2011 -0700| || |Merge pull request #4 from gotgithub/master| || |Find a typo in the book| || * commit 7ecdfe7451412cfb2e65bb47c12cf2162e21c841|/ Author: Wang Sheng <wangsheng@ossxp.com>| Date: Tue Aug 16 10:17:53 2011 +0800||Fixed #3: should be 项目, not 项.|* commit 00c6c4bfab9824bd967440902ce87440f9e87852| Author: Jiang Xin <worldhello.net@gmail.com>| Date: Wed Aug 3 11:50:31 2011 +0800||Change font color for stronger text from red to brown.
可以看出GitHub的自动合并产生了一个合并提交,类似执行gitmerge–no-ff命令。也就是说即使用户wangsheng的提交是一个“快进式提交”(基于gotgit/gotgit版本库最新提交所做的提交),也要产生一个合并提交。
可能有人并不喜欢这种用–no-ff参数的非标准的合并方式,因为这种合并产生了一个多余的提交,可能增加代码评审的负担。若要取消GitHub的自动合并也很简单,因为Git无所不能:
$ git reset –hard HEAD^ # 回退一个提交,即回退到当前提交的第一个父提交$ git rev-parse HEAD# 检查是否正确的回退00c6c4bfab9824bd967440902ce87440f9e87852$ git push -f# 强制推送回退的 master 分支
下面就演示一下当收到他人的Pull Request后,该如何手动合并。实际上在很多情况下,Pull Request所含提交有可能造成合并冲突,那样的话GitHub不再、也不能提供自动合并功能,就必须采用手工合并的方式。
将Pull Request发出者的派生版本库添加为一个新的源。
例如收到来自gotgithub用户的Pull Request,不妨以gotgithub为名添加新的源。
$ git remote add gotgithub https://github.com/gotgithub/gotgit.git
此时版本库中有两个源,,一个克隆时自动建立的origin,另外一个就是新增加的gotgithub。
$ git remote -vgotgithubhttps://github.com/gotgithub/gotgit.git (fetch)gotgithubhttps://github.com/gotgithub/gotgit.git (push)origin git@github.com:gotgit/gotgit.git (fetch)origin git@github.com:gotgit/gotgit.git (push)
获取远程版本库gotgithub的分支和提交。
$ git fetch gotgithubFrom https://github.com/gotgithub/gotgit * [new branch]gh-pages -> gotgithub/gh-pages * [new branch]master-> gotgithub/master
现在除了本地分支master外,还有若干远程分支,如下:
$ git branch -a* master remotes/gotgithub/gh-pages remotes/gotgithub/master remotes/origin/HEAD -> origin/master remotes/origin/gh-pages remotes/origin/master
将远程分支remotes/gotgithub/master(可简写为gotgithub/master)合并到当前分支中。
$ git merge gotgithub/masterUpdating 00c6c4b..7ecdfe7Fast-forward errata.mkd | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
查看提交说明,看到此次合并没有产生不必要的合并提交。
$ git log –graph -2* commit 7ecdfe7451412cfb2e65bb47c12cf2162e21c841| Author: Wang Sheng <wangsheng@ossxp.com>| Date: Tue Aug 16 10:17:53 2011 +0800||Fixed #3: should be 项目, not 项.|* commit 00c6c4bfab9824bd967440902ce87440f9e87852| Author: Jiang Xin <worldhello.net@gmail.com>| Date: Wed Aug 3 11:50:31 2011 +0800||Change font color for stronger text from red to brown.
将合并推送到GitHub版本库中。
$ git push
有希望在的地方,痛苦也成欢乐