【问题标题】:Git - rollback to previous commitGit - 回滚到上一个提交
【发布时间】:2012-12-02 22:36:10
【问题描述】:

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

【问题讨论】:

    标签: git github


    【解决方案1】:

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

    git push origin HEAD~1:master
    

    解释:

    • origin 是远程仓库的名称
    • HEAD~1 是源参考规范——要推送的修订。 HEAD~1 表示在当前本地 HEAD 之后提交一次。
    • master 是 target-refspec – 要推送到的远程分支。

    【讨论】:

      【解决方案2】:

      答案取自这里: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.
      

      【讨论】:

        猜你喜欢
        • 2021-05-27
        • 1970-01-01
        • 2020-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 2011-05-21
        相关资源
        最近更新 更多