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

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

问题描述

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

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

如何撤消本地存储库中的那些提交?

How can I undo those commits from the local repository?

推荐答案

撤消提交并重做

$ git commit -m "Something terribly misguided"             # (1)
$ git reset HEAD~                                          # (2)
<< edit files as necessary >>                              # (3)
$ git add ...                                              # (4)
$ git commit -c ORIG_HEAD                                  # (5)

  1. 这是您要撤消的操作.
  2. 这不会对您的工作树(磁盘上文件的状态)产生任何影响,但是会撤消提交,并使您提交的更改未进行暂存(因此,它们将在git status中显示为未暂存为提交的更改",因此您需要在提交之前再次添加它们).如果仅 要对先前的提交进行添加更多更改,或更改提交消息 1 ,则可以使用git reset --soft HEAD~代替,就像git reset HEAD~ 2 一样,但是保留您现有的更改.
  3. 对工作树文件进行更正.
  4. git add您想要包含在新提交中的任何内容.
  5. 提交更改,重新使用旧的提交消息. reset将旧的头复制到.git/ORIG_HEADcommit-c ORIG_HEAD将打开一个编辑器,该编辑器最初包含旧提交的日志消息,并允许您对其进行编辑.如果不需要编辑消息,则可以使用-C选项.
  1. This is what you want to undo.
  2. This does nothing to your working tree (the state of your files on disk), but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status, so you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~2 but leaves your existing changes staged.
  3. Make corrections to working tree files.
  4. git add anything that you want to include in your new commit.
  5. 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.

但是请注意,如果您对索引添加了任何新更改,则使用commit --amend会将它们添加到以前的提交中.

Beware, however, that if you have added any new changes to the index, using commit --amend will add them to your previous commit.

如果代码已经推送到您的服务器,并且您有权覆盖历史记录(变基),则:

If the code is already pushed to your server and you have permissions to overwrite history (rebase) then:

git push origin master --force

您也可以查看以下答案:

You can also look at this answer:

我如何将HEAD移回先前的位置? (独立的头)&撤消提交

上面的答案将向您显示git reflog,,用于查找什么是SHA-1,您希望还原该SHA-1.找到要撤消的点后,即可使用上述命令序列.

The above answer will show you git reflog, which is used to find out what is the SHA-1, which you wish to revert. Once you found the point to which you want to undo to use the sequence of commands as explained above.

1 但是,请注意,如果您在 commit消息中犯了一个错误,则无需重置为较早的提交.较简单的选项是git reset(取消分段自此以来所做的任何更改),然后 git commit --amend ,将打开您的默认提交消息编辑器,该编辑器已预先填充了最后一条提交消息.

1 Note, however, that you don't need to reset to an earlier commit if you just made a mistake in your commit message. The easier option is to git reset (to unstage any changes you've made since) and then git commit --amend, which will open your default commit message editor pre-populated with the last commit message.

2 HEAD~HEAD~1相同.另外,请参见 git中的HEAD是什么?.如果您要取消提交多个提交,这将很有帮助.

2 HEAD~ is the same as HEAD~1. Also, see What is the HEAD in git?. It's helpful if you want to uncommit multiple commits.

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

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