合并另一个远程分支的Git操作

42

现在,我看到很多来自Linus Torvalds和/或Gitster的提交看起来像这样:

Merge branch 'maint' of git://github.com/git-l10n/git-po into maint …
* 'maint' of git://github.com/git-l10n/git-po:
  l10n: de.po: fix a few minor typos

或:

Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/ralf/upst… …
…ream-linus

Pull MIPS update from Ralf Baechle:
...
* 'upstream' of git://git.linux-mips.org/pub/scm/ralf/upstream-linus: (22 commits)
  MIPS: SNI: Switch RM400 serial to SCCNXP driver
...

我不知道怎么做,但是我知道关于git remote、git checkout和git merge的知识,我用它们合并分支(或“拉取请求”),但它不会生成这样的消息,为什么?有人可以提供一些例子吗?

P.S:我是Linus Torvalds提交等的粉丝,喜欢他的合并描述有多详细;P

P.S:这就是我以前合并东西的方式:

git remote add anotherremoot
git checkout -b anotherbranch
git pull remoteshortcut
...do tests...
git checkout master
git merge anotherbranch
git push

我从这里学到了上面的示例:http://viget.com/extend/i-have-a-pull-request-on-github-now-what


2
这是Linus Torvalds。请纠正您帖子的第一行 :) - Brod
1个回答

72

使用git pull命令将远程分支合并到master分支:

git remote add something git://host.example.com/path/to/repo.git
git checkout master
git pull something master

或者不添加远程地址也可以这样做:

git pull git://host.example.com/path/to/repo.git

git pull 命令会拉取远程分支并合并到本地分支。如果可能的话,它会进行快进合并,即仅仅更新当前主分支到最新提交,不生成合并提交。但如果两边都有修改,则会执行合并操作,就像你看到 Linus 和 Junio 执行的那样,并包含远程 URL。

如果你想确保获得一个合并提交,即使可以进行快进合并,也可以使用 git pull --no-ff。如果你想确保 pull 永远不会创建合并提交(因此在两边都有更改时失败),请使用 git pull --ff-only

如果你想包含更详细的消息,比如 Linus 提供的完整日志,请使用 git pull --log。如果你想编辑消息,而不仅仅使用自动创建的消息,则使用 git pull --edit。有关更多选项,请参见 git pull 文档


4
我很好奇为什么这里无法使用 git merge。有人知道吗? - Nick Ribal
1
@NickRibal 普通的合并会导致“fatal: refusing to merge unrelated histories”错误,但我们可以使用“git merge otherremote/otherbranch --allow-unrelated-histories”命令进行合并。我确认我已经这样做了,它可以正常工作。 - Mavaddat Javid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接