虽然我的回答超出了您的问题,但我认为这实际上是您想要做的事情。
您使用了“git reset --soft HEAD^”来撤销您的提交。这将工作副本返回到提交之前的状态(因为“HEAD”指向当前提交,“HEAD^”指向它之前的提交(假设只有一个父项)。
但现在,当您执行“git push”时,会收到以下类似的消息:
! [rejected] <branch> -> <branch>e (non-fast-forward)
error: failed to push some refs to 'ssh://<remote server>/<remote path>'
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.
这意味着提交不匹配,旨在防止您犯错误。错误消息有点误导,并且您不希望执行其建议(拉取以使分支同步)。只有通过您的意图,才能知道不要这样做。
您可以使用--force
(或-f
)(*)简单地解决此问题:
git push --force
您可能需要重新设置上游:
git push --force --set-upstream origin <branch>
请注意,如果其他人已经拉取了你的工作,这将产生后果,因为将会有不同的提交正在进行相同的更改(可能)。详情请参见
https://www.kernel.org/pub/software/scm/git/docs/user-manual.html#problems-With-rewriting-history。
为了避免任何问题,请仅对您的分支进行推送(而不是一些公共分支,例如开发人员合并所有功能分支的
development
分支),并确保在团队中有
开放交流。
开发人员通常会使用此模式来进行所谓的
周五下午提交,其中您希望在周末之前保存您的工作以防硬件故障(但在周一返回到预提交状态)。
*Friday*
git add --all # To add all files whether they are tracked or not
git commit -m "Friday afternoon commit"
git --set-upstream push # --set-upstream is for if the branch doesn't exist on the remote server
*Monday*
git reset --soft HEAD^
git push -f --set-upstream origin <branch>
与另一个答案中讨论的“git revert”相比,以这种方式操作的优点在于避免了额外的提交。重置将有两个提交,而这种方法将没有(没有额外的提交)。使用“git reset”的优点是它不会重写历史记录,因此更加安全,特别是如果您不确定自己在做什么时。通常情况下,存储库被配置为不允许您对主分支进行此操作 - 请在分支上进行修复并创建拉取请求。因为如果您已经阅读了上面的链接,则在主分支上重写历史记录将产生严重后果(除非您是唯一克隆该代码的人)。
$ git reset --soft formerCommit
之后会回到先前的状态,实际上 HEAD 指向了之前的提交,但是当我尝试推送新更改时,我遇到了与问题中相同的错误,因为 HEAD 已经分叉,所以我不明白其中的优势在哪里? - Arpan Banerjee