当我 运行 "git push" 我得到 "tip of your current branch is behind its remote" 但当前分支没有上游跟踪分支

When I run "git push" I get "tip of your current branch is behind its remote" but the current branche has no upstream tracking 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.

所以我 运行 : 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

您有一个名为X的本地分支,没有跟踪信息,所以没有相应的upstream

那么您正试图将其作为 X 推送到 origin。到目前为止一切顺利...但是那个远程中有一个同名的分支,所以你的推送只有在快进时才会被接受,而事实并非如此。

当您执行 git pull 时,您尝试 merge/rebase 跟踪分支,但您的 X 没有!所以它失败了。

你基本上有两个选择:

  1. 添加跟踪信息:如 git 在控制台中有用地打印:git branch --setup-upstream-to=origin/X X,然后是 git pullgit statusgit mergegit rebse, git push 没有参数将默认为跟踪分支,它将按预期工作。
  2. 请拒绝添加跟踪信息。然后您必须执行完整的命令:git fetch origin,然后是 git rebase origin/Xgit merge origin/X,以及 git push origin X.

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