当我运行“git push”时,出现“您当前分支的顶端落后于远程”,但当前分支没有上游跟踪分支。

4

我正在处理本地分支X,当我尝试使用git push -u origin X推送时,出现以下错误信息:

! [rejected]        X -> X (non-fast-forward)
error: failed to push some refs to "********"
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

所以我运行:git pull 然后出现了一个错误信息:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> X
1个回答

15

你有一个名为X的本地分支,没有跟踪信息,因此没有对应的上游分支。

然后你试图将它推送到远程仓库的origin上作为X。目前为止一切顺利...但是在该远程仓库中有一个同名的分支,所以只有在快进的情况下才会接受你的推送,而实际情况并非如此。

当你运行git pull时,你试图合并/变基跟踪分支,但是你的X没有跟踪分支,所以失败了。

你基本上有两个选择:

  1. 添加跟踪信息:正如git在控制台中有用地打印出来的那样:git branch --set-upstream-to=origin/X X,然后git pullgit statusgit mergegit rebasegit push不带参数将默认使用跟踪分支,并且它将按预期工作。
  2. 拒绝添加跟踪信息。然后你必须执行完整的命令:git fetch origin,然后git rebase origin/Xgit merge origin/X,以及git push origin X

既然你正在尝试使用git push -u,而-u的意思是“添加跟踪信息”,我想你想选择选项1。


git branch --set-upstream-to=origin/X X 完成了任务,此时我只需要解决合并冲突,提交并推送即可。 - moe_
我认为你的意思是 git branch --set-upstream-to=origin/X X in 1。 - Jesse Roper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接