当我运行“git Push”时,我得到“当前分支的尖端位于其远程后面”,但当前分支没有上游跟踪分支

moe*_*oe_ 4 git push pull github git-branch

我正在这个本地分支 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.
Run Code Online (Sandbox Code Playgroud)

所以我运行: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
Run Code Online (Sandbox Code Playgroud)

rod*_*igo 15

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

然后你试图将它推到originas X。到目前为止一切顺利...但是该遥控器中有一个具有该名称的分支,因此只有在快进时您的推送才会被接受,但事实并非如此。

当您这样做时,git pull您会尝试合并/变基跟踪分支,但您X没有!所以它失败了。

您基本上有两个选择:

  1. 添加跟踪信息:因为 git 在控制台中有用地打印:git branch --set-upstream-to=origin/X X,然后git pull, git status, git merge, git rebse,git push不带参数将默认为跟踪分支,并且它将按预期工作。
  2. 请拒绝添加跟踪信息。然后您必须执行完整命令:git fetch origin、thengit rebase origin/Xgit merge origin/X、and git push origin X

由于您正在尝试git push -u, 的-u意思是“添加跟踪信息”,我猜您需要选项 1。