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

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


当前回答

你也可以

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 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 branch -a

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

git签出分行//进入分行

gitcheckout-b“yourNewBranch”//在基本“theBranch”中的自己的分支中工作

简单地说,如果你想从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

这有助于我在将其合并到其他分支之前获取远程分支:

git fetch repo xyz:xyz
git checkout xyz