假设有人创建了一个分支xyz。如何从远程服务器(例如GitHub)中提取分支xyz并将其合并到本地存储库中的现有分支xyz中?

答案推送分支到Git会给我一个错误“![拒绝]”,并提到“非快进”。


当前回答

只需显式跟踪远程分支,一个简单的git pull就能满足您的需求:

git branch -f remote_branch_name origin/remote_branch_name
git checkout remote_branch_name

后者是本地操作。

或者更符合GitHub关于分叉的文档:

git branch -f new_local_branch_name upstream/remote_branch_name

其他回答

git pull <gitreponame> <branchname>

通常,如果您只有repo分配给代码,那么gitrepname将是源代码。

如果您正在处理两个回购,比如一个是本地的,另一个是远程的,您可以从gitremote-v查看回购列表。这显示了分配给当前代码的回购数量。

BranchName应存在于相应的gitrepname中。

您可以使用以下两个命令添加或删除repo

git remote add <gitreponame> <repourl>
git remote remove <gitreponame>

最好的方法是:

git checkout -b <new_branch> <remote repo name>/<new_branch>

你也可以

git pull -r origin master

修复合并冲突(如果有)

git rebase --continue

-r表示重基。这将使您从

        v  master       
o-o-o-o-o
     \o-o-o
          ^ other branch

to

        v  master       
o-o-o-o-o-o-o-o
              ^ other branch

这将导致一个更干净的历史。注意:如果您已经将另一个分支推送到源(或任何其他远程),则可能必须在重新设置基之后强制推送分支。

git push -f origin other-branch

可以使用

git checkout --track origin/the-branch-name

确保分支名称完全相同。

我做的

git branch -f new_local_branch_name origin/remote_branch_name

而不是

git branch -f new_local_branch_name upstream/remote_branch_name

正如@innaM所建议的。当我使用上游版本时,它说“fatal:不是有效的对象名:”upstream/remote_branch_name“”。我并没有按照注释的建议执行gitfetchorigin,而是简单地将上游替换为origin。我想它们是等价的。