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

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


当前回答

最好的方法是:

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

其他回答

一种安全的方法是首先创建本地分支(即xyz),然后将远程分支拉入本地。

# create a local branch
git checkout -b xyz

# make sure you are on the newly created branch
git branch

# finally pull the remote branch to your local branch
git pull origin xyz

下面是可以将远程分支拉到本地分支的语法。

git pull {repo} {remotebranchname}:{localbranchname}

git pull origin xyz:xyz

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

现在您可以用git签出MyNewBranch

完成时间:)


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

可以使用

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

确保分支名称完全相同。

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

git fetch repo xyz:xyz
git checkout xyz

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

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