如何撤消 Git 中最近的本地提交? [英] How do I undo the most recent local commits in Git?

查看:49
本文介绍了如何撤消 Git 中最近的本地提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心Git提交了错误的文件,但没有尚未将提交推送到服务器.

我如何从本地存储库中撤消这些提交?

似乎唯一的方法是在某种 GUI 文本编辑器中复制编辑,然后擦除整个本地克隆,然后重新克隆存储库,然后重新应用编辑.然而,

  • 这可能会导致数据丢失.
  • 如果只运行了一次意外的 git commit,这很难做到.

有更好的方法吗?

解决方案

撤消提交 &重做

$ git commit -m 一些非常误导的东西"#(0:你的意外)$ git reset HEAD~#(1)[根据需要编辑文件]#(2)$ git 添加.# (3)$ git commit -c ORIG_HEAD # (4)

  1. 此命令负责撤消.它会撤消您的上次提交,同时保持您的工作树(磁盘上文件的状态)不变.您需要再次添加它们,然后才能再次提交它们).

  2. 对工作树文件进行更正.

  3. git add 任何您想包含在新提交中的内容.

  4. 提交更改,重用旧的提交消息.reset 把旧的head复制到.git/ORIG_HEADcommit with -c ORIG_HEAD 将打开一个编辑器,它最初包含来自旧提交的日志消息并允许您对其进行编辑.如果您不需要编辑消息,您可以使用 -C 选项.

或者,要编辑先前的提交(或只是其提交消息)commit --amend 会将当前索引中的更改添加到先前的提交中.>

要删除(而不是还原)已推送到服务器的提交,需要使用 git push origin master --force 重写历史记录.


进一步阅读

我该怎么办将 HEAD 移回以前的位置?(分离头) &撤消提交

上面的答案将显示 git reflog,您可以使用它来确定要还原到的提交的 SHA-1.获得此值后,请使用上述命令序列.


HEAD~HEAD~1 相同.文章 git 中的 HEAD 是什么? 如果您想取消提交多次提交会很有帮助.

I accidentally committed the wrong files to Git, but didn't push the commit to the server yet.

How can I undo those commits from the local repository?

The only way seems to be to copy the edits in some kind of GUI text editor, then wipe the whole local clone, then re-clone the repository, then re-applying the edits. However,

  • This can cause data loss.
  • It's very hard to do this when only an accidental git commit was run.

Is there a better way?

解决方案

Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)

  1. This command is responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again).

  2. Make corrections to working tree files.

  3. git add anything that you want to include in your new commit.

  4. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin master --force is necessary.


Further Reading

How can I move HEAD back to a previous location? (Detached head) & Undo commits

The above answer will show you git reflog, which you can use to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

这篇关于如何撤消 Git 中最近的本地提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆