如何撤消Git中的最新提交? [英] How to undo the most recent commits in Git?

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

问题描述

我意外地提交 - 将错误的文件复制到 Git ,但是我还没有 push - 将服务提交到服务器。

I accidentally commit-ed wrong files to Git, but I haven't push-ed the commit to the server yet.

如何从本地资源库中取消这些提交?

How can I undo those commits from the local repository?

推荐答案

撤消提交并重做



Undo a commit and redo

$ 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〜(其中 HEAD〜 HEAD〜1 相同),但会保留现有更改。
  3. 更正工作树文件。 / li>
  4. git add 您希望包含在新提交中的任何内容。 提交更改,重用旧的提交消息。 reset 将旧头复制到 .git / ORIG_HEAD ; commit -c ORIG_HEAD 将打开一个编辑器,它最初包含来自旧提交的日志消息并允许您编辑它。如果您不需要编辑邮件,则可以使用 -C 选项。

  1. This is what you want to undo
  2. This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status, and 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~ (where HEAD~ is the same as HEAD~1) 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.






1 但是,请注意,如果您在标记中犯了错误,则无需重置为以前的提交。提交消息。更简单的选择是 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 upstage 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.

然而,请注意,如果您已将任何新更改添加到索引中,则使用 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.

----编辑Frank R. 2018-3-9

---- Edit by Frank R. 2018-3-9

如果推送,

If pushed,

git push origin master --force

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

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