如何撤消 Git 中最近的本地提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/927358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I undo the most recent local commits in Git?
提问by Hamza Yerlikaya
回答by Esko Luontola
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)
- This is what you want to undo.
- 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 onlywant to addmore changes to the previous commit, or change the commit message1, you could usegit reset --soft HEAD~
instead, which is likegit reset HEAD~
2but leaves your existing changes staged. - Make corrections to working tree files.
git add
anything that you want to include in your new commit.- 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.
- 这是您要撤消的操作。
- 这对您的工作树(磁盘上文件的状态)没有任何影响,但会撤消提交并使您提交的更改未暂存(因此它们将在 中显示为“未暂存以进行提交的更改”
git status
,因此您需要在提交之前再次添加它们)。如果你只需要添加更多的变化上一次提交,或更改提交信息1,您可以使用git reset --soft HEAD~
替代,这就好比是git reset HEAD~
2,但离开你现有的变化上演。 - 对工作树文件进行更正。
git add
您想要包含在新提交中的任何内容。- 提交更改,重用旧的提交消息。
reset
将旧头复制到.git/ORIG_HEAD
;commit
with-c ORIG_HEAD
将打开一个编辑器,它最初包含来自旧提交的日志消息,并允许您对其进行编辑。如果您不需要编辑消息,则可以使用该-C
选项。
Beware, however, that if you have added any new changes to the index, using commit --amend
will add them to your previous commit.
但是请注意,如果您向索引添加了任何新更改, usingcommit --amend
会将它们添加到您之前的提交中。
If the code is already pushed to your server and you have permissions to overwrite history (rebase) then:
如果代码已经推送到您的服务器并且您有权覆盖历史记录(rebase),则:
git push origin master --force
You can also look at this answer:
你也可以看看这个答案:
How can I move HEAD back to a previous location? (Detached head) & Undo commits
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.
上面的答案将告诉您git reflog,
哪个用于找出您希望还原的 SHA-1。找到要撤消的点后,即可使用上述命令序列。
1Note, 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.
1但是请注意,如果您只是在提交消息中犯了错误,则无需重置为较早的提交。更简单的选择是git reset
(取消您此后所做的任何更改)然后git commit --amend
,这将打开您的默认提交消息编辑器,其中预先填充了最后一条提交消息。
2HEAD~
is the same as HEAD~1
. Also, see What is the HEAD in git?. It's helpful if you want to uncommit multiple commits.
2HEAD~
与 相同HEAD~1
。另外,请参阅git 中的 HEAD 是什么?. 如果您想取消提交多个提交,这会很有帮助。
回答by Ryan Lundy
Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand.
如果您不知道它是如何工作的,那么撤消提交有点可怕。但是,如果您确实理解,它实际上非常容易。
Say you have this, where C is your HEAD and (F) is the state of your files.
假设您有这个,其中 C 是您的 HEAD, (F) 是您的文件状态。
(F)
A-B-C
↑
master
You want to nuke commit C and never see it again and lose all the changes in locally modified files. You do this:
您想取消提交 C 并且再也看不到它并丢失本地修改文件中的所有更改。你做这个:
git reset --hard HEAD~1
The result is:
结果是:
(F)
A-B
↑
master
Now B is the HEAD. Because you used --hard
, your files are reset to their state at commit B.
现在 B 是 HEAD。因为您使用了--hard
,您的文件将重置为提交 B 时的状态。
Ah, but suppose commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changesfor a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:
啊,但假设提交 C 不是一场灾难,只是有点偏离。您想撤消提交,但在进行更好的提交之前保留您的更改以进行一些编辑。从这里重新开始,以 C 为 HEAD:
(F)
A-B-C
↑
master
You can do this, leaving off the --hard
:
您可以执行此操作,而无需执行以下操作--hard
:
git reset HEAD~1
In this case the result is:
在这种情况下,结果是:
(F)
A-B-C
↑
master
In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1
, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard
) you leave your files as they were. So now git status
shows the changes you had checked into C. You haven't lost a thing!
在这两种情况下, HEAD 只是指向最新提交的指针。当你执行 a 时git reset HEAD~1
,你告诉 Git 将 HEAD 指针移回一次提交。但是(除非您使用--hard
),您将文件保持原样。所以现在git status
显示您已签入 C 的更改。您没有丢失任何东西!
For the lightest touch, you can even undo your commit but leave your files and your index:
对于最轻的接触,您甚至可以撤消您的提交,但保留您的文件和索引:
git reset --soft HEAD~1
This not only leaves your files alone, it even leaves your indexalone. When you do git status
, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit
and you'd be redoing the same commit you just had.
这不仅让您的文件独自一人,甚至让您的索引独自一人。当你这样做时git status
,你会看到索引中的文件和以前一样。事实上,在这个命令之后,你可以做git commit
,你会重做你刚才所做的同样的提交。
One more thing: Suppose you destroy a commitas in the first example, but then discover you needed it after all? Tough luck, right?
还有一件事:假设你像第一个例子那样销毁一个提交,但后来发现你毕竟需要它?运气不好,对吧?
Nope, there's stilla way to get it back. Type git reflog
and you'll see a list of (partial) commit shas(that is, hashes) that you've moved around in. Find the commit you destroyed, and do this:
不行,还是有办法找回来的。键入git reflog
,您将看到您移动的(部分)提交shas(即哈希)列表。找到您销毁的提交,然后执行以下操作:
git checkout -b someNewBranchName shaYouDestroyed
You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.
你现在已经恢复了那个提交。提交实际上不会在 Git 中被销毁大约 90 天,因此您通常可以返回并拯救一个您不想摆脱的提交。
回答by Andrew
There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository):
有两种方法可以“撤消”上次提交,具体取决于您是否已经公开提交(推送到远程存储库):
How to undo a local commit
如何撤消本地提交
Let's say I committed locally, but now I want to remove that commit.
假设我在本地提交,但现在我想删除该提交。
git log
commit 101: bad commit # Latest commit. This would be called 'HEAD'.
commit 100: good commit # Second to last commit. This is the one we want.
To restore everything back to the way it was prior to the last commit, we need to reset
to the commit before HEAD
:
要将所有内容恢复到上次提交之前的状态,我们需要reset
在提交之前HEAD
:
git reset --soft HEAD^ # Use --soft if you want to keep your changes
git reset --hard HEAD^ # Use --hard if you don't care about keeping the changes you made
Now git log
will show that our last commit has been removed.
现在git log
将显示我们的最后一次提交已被删除。
How to undo a public commit
如何撤消公开提交
If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).
如果您已经公开提交,您将需要创建一个新提交,该提交将“恢复”您在之前提交(当前 HEAD)中所做的更改。
git revert HEAD
Your changes will now be reverted and ready for you to commit:
您的更改现在将被还原并准备好让您提交:
git commit -m 'restoring the file I removed by accident'
git log
commit 102: restoring the file I removed by accident
commit 101: removing a file we don't need
commit 100: adding a file that we need
For more information, check out Git Basics - Undoing Things.
有关更多信息,请查看Git 基础知识 - 撤销操作。
回答by bdonlan
Add/remove files to get things the way you want:
添加/删除文件以按照您想要的方式获取内容:
git rm classdir
git add sourcedir
Then amend the commit:
然后修改提交:
git commit --amend
The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place.
之前的错误提交将被编辑以反映新的索引状态 - 换句话说,这就像你从一开始就没有犯过错误。
Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.
请注意,只有在尚未推送时才应执行此操作。如果你已经推送,那么你只需要正常提交修复。
回答by Lennart Koopmann
git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"
or
或者
git reset --hard HEAD~1
Warning: The above command will permanently remove the modifications to the .java
files (and any other files) that you wanted to commit.
警告:上述命令将永久删除对.java
您要提交的文件(和任何其他文件)的修改。
The hard reset
to HEAD-1
will set your working copy to the state of the commit before your wrong commit.
该hard reset
来HEAD-1
将你的工作拷贝设定在状态的你的不对了提交前提交。
回答by Zaz
To change the last commit
更改最后一次提交
Replace the files in the index:
替换索引中的文件:
git rm --cached *.class
git add *.java
Then, if it's a private branch, amendthe commit:
然后,如果是私有分支,修改提交:
git commit --amend
Or, if it's a shared branch, make a new commit:
或者,如果它是共享分支,请进行新的提交:
git commit -m 'Replace .class files with .java files'
(To change a previous commit, use the awesome interactive rebase.)
(要更改以前的提交,请使用很棒的交互式 rebase。)
ProTip?: Add *.class
to a gitignoreto stop this happening again.
专业提示?:添加*.class
到gitignore以阻止这种情况再次发生。
To revert a commit
还原提交
Amending a commit is the ideal solution if you need to change the last commit, but a more general solution is reset
.
如果您需要更改上次提交,则修改提交是理想的解决方案,但更通用的解决方案是reset
.
You can reset Git to any commit with:
您可以使用以下命令将 Git 重置为任何提交:
git reset @~N
Where N
is the number of commits before HEAD
, and @~
resets to the previous commit.
哪里N
是之前的提交次数HEAD
,并@~
重置为之前的提交。
So, instead of amending the commit, you could use:
因此,您可以使用:
git reset @~
git add *.java
git commit -m "Add .java files"
Check out git help reset
, specifically the sections on --soft
--mixed
and --hard
, for a better understanding of what this does.
查看git help reset
,特别是有关--soft
--mixed
和的部分--hard
,以便更好地了解它的作用。
Reflog
参考日志
If you mess up, you can always use the reflog to find dropped commits:
如果你搞砸了,你总是可以使用 reflog 来查找删除的提交:
$ git reset @~
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~
2c52489 HEAD@{1}: commit: added some .class files
$ git reset 2c52489
... and you're back where you started
回答by Jaco Pretorius
Use git revert <commit-id>
.
使用git revert <commit-id>
.
To get the commit ID, just use git log
.
要获取提交 ID,只需使用git log
.
回答by Madhan Ayyasamy
If you are planning to undo a local commit entirely, whatever you change you did on the commit, and if you don't worry anything about that, just do the following command.
如果您打算完全撤消本地提交,无论您在提交时做了什么更改,如果您对此不担心,只需执行以下命令。
git reset --hard HEAD^1
(This command will ignore your entire commit and your changes will be lost completely from your local working tree). If you want to undo your commit, but you want your changes in the staging area (before commit just like after git add
) then do the following command.
(此命令将忽略您的整个提交,并且您的更改将从本地工作树中完全丢失)。如果您想撤消您的提交,但您希望在暂存区中进行更改(提交之前就像之后一样git add
),请执行以下命令。
git reset --soft HEAD^1
Now your committed files come into the staging area. Suppose if you want to upstage the files, because you need to edit some wrong content, then do the following command
现在您提交的文件进入暂存区。假设如果你想升级文件,因为你需要编辑一些错误的内容,那么执行以下命令
git reset HEAD
Now committed files to come from the staged area into the unstaged area. Now files are ready to edit, so whatever you change, you want to go edit and added it and make a fresh/new commit.
现在将来自暂存区的文件提交到未暂存区。现在文件已准备好进行编辑,因此无论您更改什么,您都想编辑并添加它并进行全新/新的提交。
回答by nickf
If you have Git Extrasinstalled, you can run git undo
to undo the latest commit. git undo 3
will undo the last three commits.
如果您安装了Git Extras,则可以运行git undo
以撤消最新提交。git undo 3
将撤消最后三个提交。
回答by neoneye
I wanted to undo the latest five commits in our shared repository. I looked up the revision id that I wanted to rollback to. Then I typed in the following.
我想撤消我们共享存储库中最新的五次提交。我查找了我想回滚到的修订 ID。然后我输入了以下内容。
prompt> git reset --hard 5a7404742c85
HEAD is now at 5a74047 Added one more page to catalogue
prompt> git push origin master --force
Total 0 (delta 0), reused 0 (delta 0)
remote: bb/acl: neoneye is allowed. accepted payload.
To [email protected]:thecompany/prometheus.git
+ 09a6480...5a74047 master -> master (forced update)
prompt>