git 变基后推送拒绝

push rejection after a git rebase

我正在开发名为 auth 的功能分支,它是从 master 分支分支出来的:

C1 (master)
 \ 
  C2-C3 (auth)

提交 C3 后,我将 auth 分支推送到远程以进行备份。然后 master 分支在远程更新,历史记录如下:

C1-C4 (master)
 \ 
  C2-C3 (auth)

我正在将 auth 分支重新设置为更新的 master 分支,历史记录现在如下所示:

C1-C4 (master)
    \
     C2-C3 (auth)

然后我在 auth 分支上做了更多更改,使 C5 提交并将 auth 分支推送到远程并且 推送被拒绝消息是远程有更新,而我在本地没有。但我知道没有其他人推送到远程的 auth 分支。那么为什么会出现这种行为?

article 仅对此类行为提供了一个简单的解释:

Beware though: if the rebased branch had been pushed to a remote (for backup purposes, for instance), you’ll need to force the next push of it with the -f option, as you just replaced its commit history with a fresh one.`

auth 分支的变基 has changed the history

这就是为什么你需要强制推送,只有你是唯一一个在这个分支上工作的人才可以。
如果没有,最好是:

  • reset auth branch to its state before the rebase
  • 合并master进去
  • 添加C5
  • 正常推送

如:

C1-----C4      (master)
 \      \
  C2-C3--M--C5 (auth)