【发布时间】:2012-12-02 22:36:10
【问题描述】:
我正在和一个合作伙伴一起做一个 git 项目。我做了一些更改,然后意外添加并提交了比我预期更多的文件,并将它们推送到主存储库。如何将远程存储库回滚到最后一次提交,但保留我的本地副本以便我可以重新添加和正确提交?
【问题讨论】:
我正在和一个合作伙伴一起做一个 git 项目。我做了一些更改,然后意外添加并提交了比我预期更多的文件,并将它们推送到主存储库。如何将远程存储库回滚到最后一次提交,但保留我的本地副本以便我可以重新添加和正确提交?
【问题讨论】:
您可以告诉git push 将遥控器推送到特定版本:
git push origin HEAD~1:master
解释:
origin 是远程仓库的名称HEAD~1 是源参考规范——要推送的修订。 HEAD~1 表示在当前本地 HEAD 之后提交一次。master 是 target-refspec – 要推送到的远程分支。【讨论】:
答案取自这里:How to undo last commit(s) in Git?
撤消提交并重做
$ git commit ... (1)
$ git reset --soft HEAD^ (2)
$ edit (3)
$ git add .... (4)
$ git commit -c ORIG_HEAD (5)
This is what you want to undo
This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".
Make corrections to working tree files.
Stage changes for commit.
"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
【讨论】: