Git - 回滚到之前的提交

时间:2012-12-02 22:36:11

标签: git github

我正在和合作伙伴一起开展一个git项目。我做了一些更改,然后意外添加并提交了比我预期更多的文件,并将它们推送到主存储库。如何将远程存储库回滚到上次提交,但是保留我的本地副本以便我可以正确地重新添加和提交?

2 个答案:

答案 0 :(得分:2)

您可以告诉git push将遥控器推送到特定版本:

git push origin HEAD~1:master

说明:

  • origin是远程仓库的名称
  • HEAD~1是source-refspec - 要推送的修订版。 HEAD~1表示当前本地HEAD后面的一次提交。
  • master是target-refspec - 要推送到的远程分支。

答案 1 :(得分:1)

从这里回答: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.