Git 关于非快速转发到分支的更改的错误

Git error about non fast forwarded changes to branch

现在我有一个名为 playGround 的 repo
它记录了 3 次提交
我的 playGround 本地副本落后于 1 次提交
我还有一个名为 SecondB 的分支,它是从我本地的 playGround 副本复制而来的。然后在 SecondB 上我做了两个更改,所以 SecondB 总共有 4 次提交。

git 远程节目来源:

HEAD branch: master
  Remote branches:
    SecondB tracked
    master  tracked
  Local branches configured for 'git pull':
    SecondB merges with remote SecondB
    master  merges with remote master
  Local refs configured for 'git push':
    SecondB pushes to SecondB (up to date)
    master  pushes to master  (local out of date)

我的问题是当我在 SecondB 分支上并尝试推送它时会给我这个错误:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'url for repo'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

除非我指定要推送的内容:git push origin SecondB 我在 SecondB

为什么我不能像在 SecondB 上那样简单地 git push 并将其推送到 SecondB 的远程副本?

好的新信息: 进一步检查错误后,似乎每次我尝试 git 推送时,它都会尝试推送到远程分支和远程 MASTER。为什么会发生这种行为?我该如何解决这个问题?

这是因为您还没有将您尝试推送的本地分支与您尝试推送的远程分支的当前 内容合并。在这种情况下,git 试图让您在推送之前合并它。通常你会 fetch/merge (或拉)然后你就可以毫无问题地推动。如果你想忘记你没有的那个修订版并让远程拥有你在本地拥有的分支,你可以使用 git push -f

当你 运行 git push 没有其他参数时,它会尝试推送所有正在跟踪远程分支的本地分支。在您的示例中,它是 masterSecondB。但是你的本地master在远程master后面,所以无法推送。这就是命令失败的原因。

如果所有跟踪分支都领先或与远程分支同步,您只能使用 git push 进行推送。

使用 push.default 配置设置更改推送的默认行为。