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

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


当前回答

gitfetch将获取最新的分支列表。

现在您可以用git签出MyNewBranch

完成时间:)


有关更多信息,请参阅docs:git fetch

其他回答

gitfetch将获取最新的分支列表。

现在您可以用git签出MyNewBranch

完成时间:)


有关更多信息,请参阅docs:git fetch

但我收到了一个错误“![拒绝]”和一些关于“非快进”的信息

这是因为Git无法将分支中的更改合并到当前的主节点中。假设您签出了分支主节点,并且希望在远程分支中合并其他分支。执行此操作时:

$ git pull origin other-branch

Git基本上是这样做的:

$ git fetch origin other-branch && git merge other-branch

也就是说,pull只是一个fetch,然后是一个merge。然而,当拉入时,Git只有在能够执行快速向前合并的情况下才会合并其他分支。快速向前合并是一种合并,其中您尝试合并的分支的头部是您要合并的分支头部的直接后代。例如,如果您有此历史树,那么合并其他分支将导致快速向前合并:

O-O-O-O-O-O
^         ^
master    other-branch

然而,这并不是一个快速的合并:

    v master
O-O-O
\
 \-O-O-O-O
         ^ other-branch

要解决问题,首先获取远程分支:

$ git fetch origin other-branch

然后将其合并到当前分支(我假设它是主分支),并修复所有合并冲突:

$ git merge origin/other-branch
# Fix merge conflicts, if they occur
# Add merge conflict fixes
$ git commit    # And commit the merge!

只需显式跟踪远程分支,一个简单的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

简单地说,如果你想从GitHub中提取分支,我希望:

git fetch origin
git branch -f the-branch-I-want origin/the-branch-I-want
git checkout the-branch-I-want

这些对我有用。

要将特定的远程分支拉到当前所在的本地分支,请执行以下操作。(其中<remote_repo>是远程存储库,<remote_branch>是您要拉取的特定远程分支)

git pull <remote_repo> <remote_branch>

e.g.

git pull origin remote_master

将特定远程分支拉到特定本地分支。(其中<local_branch>是您要拉入的特定本地分支)

git pull <remote_repo> <remote_branch>:<local_branch>

e.g.

git pull origin remote_master:local_master