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

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


当前回答

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

git fetch repo xyz:xyz
git checkout xyz

其他回答

这些对我有用。

要将特定的远程分支拉到当前所在的本地分支,请执行以下操作。(其中<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 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。我想它们是等价的。

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

现在您可以用git签出MyNewBranch

完成时间:)


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

我不确定我是否完全理解这个问题,但拉动现有分支是这样做的(至少对我来说是这样的:)

git pull origin BRANCH

这是假设您的本地分支是从原始分支创建的。

可以使用

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

确保分支名称完全相同。