虽然我的回答是超出你的要求,我认为这实际上是你打算做的。您使用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
的好处,是避免多余的提交。重置将有2个提交,这种方式将没有(没有额外的)。 git reset
的优势在于它不会重写历史记录,所以更安全,特别是如果您不确定自己在做什么。
(*)通常将存储库配置为不让您执行此操作以便掌握 - 修复分支并创建拉取请求。因为如果你已经阅读过上面的链接,重写master的历史将产生严重的后果(除非你是唯一一个克隆该代码的人)。
谢谢,这是有道理的,虽然这可能会更好,因为它并没有真正解决我的问题。不过谢谢。 – 2010-01-06 22:10:13