标签云

微信群

扫码加入我们

WeChat QR Code

如何撤消最近犯的蠢货吗?

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?


Warning: you should only do this if you have not yet pushed the commit to a remote, otherwise you will mess up the history of others who have already pulled the commit from the remote!

2018年05月23日36分29秒

Here's a very clear and thorough post about undoing things in git, straight from Github.

2018年05月23日36分29秒

See this guide for Git commits undo on Local, Public and Git Branch How to undo Git Commits like pro

2018年05月23日36分29秒

Before you post a new answer, consider there are already 65+ answers for this question. Make sure that your answer contributes what is not among existing answers.

2018年05月23日36分29秒

What thSoft said is correct, pushing your commits and then git reset --bla foo requires you a git --force push [--all] [somewhere-else-than-origin]. Note that this is a "forced-push" which requires other people do a "forced-update". They should not use git pull as this implies a merge, but use git fetch [--all] and then git rebase origin/master (assume that the forced-update came from origin). All [bla] are optional parameters but sometimes needed, for example when you push to more than just one remote repository.

2018年05月23日36分29秒

And if the commit was to the wrong branch, you may git checkout theRightBranch with all the changes stages. As I just had to do.

2018年05月23日36分29秒

If you're working in DOS, instead of git reset --soft HEAD^ you'll need to use git reset --soft HEAD~1. The ^ is a continuation character in DOS so it won't work properly. Also, --soft is the default, so you can omit it if you like and just say git reset HEAD~1.

2018年05月24日36分29秒

Also, in zsh you have to quote ^, so git reset --soft 'HEAD^'... at least I did

2018年05月23日36分29秒

(Correction to what I wrote above; --mixed is the default. --mixed means to keep the changed files, but not keep them in the index. --soft would keep the changed files and keep them in the index as they were just before the changed commit. Sorry for the confusion.)

2018年05月23日36分29秒

zsh users might get: zsh: no matches found: HEAD^ - you need to escape ^ i.e. git reset --soft HEAD\^

2018年05月23日36分29秒

dma_k, yes. Or you could do git reset --hard HEAD^^ once. I use the tilde (~) notation because the caret (^) notation doesn't work in DOS.

2018年05月23日36分29秒

Another nice tip: You can re-attach the branch to the commit that you removed it from with git branch -f <branch> <commit-id>. Saves having to re-create commits!

2018年05月23日36分29秒

For a git beginner, it isn't obvious what the difference is between the last two options (--soft and the one above it). Mentioning the index doesn't help, we don't really know what that means yet. nessur's connection between soft and Ctrl-Z really helped! But I still don't quite understand the difference between the two options.

2018年05月23日36分29秒

It's much better to be told 'why' something works, than just to be told the answer. Kudos to this description - it helped be 'get' git.

2018年05月23日36分29秒

Missing a crucial point: If the said commit was previously 'pushed' to the remote, any 'undo' operation, no matter how simple, will cause enormous pain and suffering to the rest of the users who have this commit in their local copy, when they do a 'git pull' in the future. So, if the commit was already 'pushed', do this instead: git revert <bad-commit-sha1-id> git push origin :

2018年05月23日36分29秒

I found this answer the clearest. git revert HEAD^ is not the previous, is the previous of the previous. I did : git revert HEAD and then push again and it worked :)

2018年05月23日36分29秒

riezebosch: your warning is in the wrong place. THIS answer doesn't mess up things, cause it correctly creates a new commit 102: "restoring the file I removed on accident"

2018年05月23日36分29秒

This was most helpful to me. I needed to know how to revert a public commit. Thanks!

2018年05月23日36分29秒

Does this work when I did a git commit --amend and what I really meant to do is a git commit?

2018年05月23日36分29秒

dbm, if you accidentally amended, use git reset --soft <oldref>, where oldref is the commit ID before the amend. You can use git reflog to identify the old commit ID. This will undo the effects of the amend, but leave changes staged. Then just do git commit to commit as a regular commit.

2018年05月23日36分29秒

Dennis, git commit --amend turns the current tree (ie, staged changes) into a commit, overwriting current HEAD. After that point, they're not considered staged anymore because they're part of the commit (ie, git diff --cached is blank), but they're not "removed" or "lost".

2018年05月23日36分29秒

"--hard" will get rid of the modified .java files in the working directory that he wanted to commit.

2018年05月23日36分29秒

You can "git stash save" working copy changes, do a hard reset and then "git stash pop" to get them back, though I suppose a soft reset would be simpler.

2018年05月23日36分29秒

git commit -a -m "" or git commit -am "" naturally! :]

2018年05月23日36分29秒

Another 'shortcut' use of stash; if you want to unstage everything (undo git add), just git stash, then git stash pop

2018年05月23日36分29秒

If you committed to the wrong branch: once reverted, switch to the correct branch and cherry-pick the commit.

2018年05月24日36分29秒

What does that mean, cherry pick the commit? In my case, I was on the wrong branch when I edited a file. I committed it then realized I was in the wrong branch. Using "git reset --soft HEAD~1" got me back to just before the commit, but now if I checkout the correct branch, how do I undo the changes to the file in wrong branch but instead make them (in the same named file) in the correct branch?

2018年05月23日36分29秒

I just utilized git revert commit-id worked like a charm. Of course then you will need to push your changes.

2018年05月23日36分29秒

I believe that would be git cherry-pick <<erroneous-commit-sha>> astronomerdave. From, Mr. Almost-2-Years-Late-to-the-Party.

2018年05月23日36分29秒

SMR, In your example, all are pointing into current HEAD only. HEAD^ = HEAD^1. As well as HEAD^1 = HEAD~1. When you use HEAD~2, there is a difference between ~ and ^ symbols. If you use ~2 means “the first parent of the first parent,” or “the grandparent”.

2018年05月23日36分29秒

clear and easy to understand, save my night !

2018年05月23日36分29秒

Apparently, I accidentally downvoted this answer, and it's too late for me to un-downvote it. Apologies.

2018年05月23日36分29秒

Rewriting history on a shared repository is generally a very bad idea. I assume you know what you're doing, I just hope future readers do too.

2018年05月23日36分29秒

Yes rollback is dangerous. Make sure that your working copy is in the desired state before you push. When pushing then the unwanted commits gets deleted permanently.

2018年05月23日36分29秒

"Just like in the real world, if you want to rewrite history, you need a conspiracy: everybody has to be 'in' on the conspiracy (at least everybody who knows about the history, i.e. everybody who has ever pulled from the branch)." Source: stackoverflow.com/a/2046748/334451

2018年05月23日36分29秒

fantastic. This worked like a charm. There should be a git option for independently working developers - where one can use GitX, or a delete button next to a commit, and a confirmation - so this process is not so enigmatic :)

2018年05月23日36分29秒

gitk --all $(git reflog | cut -c1-7)& may be helpful for finding the previous revision if you want to undo an '--amend' commit.

2018年05月23日36分29秒

It should be noted that if you're attempting to remove secret information before pushing to a shared repository, doing a revert won't help you, because the information will still be in the history in the previous commit. If you want to ensure the change is never visible to others you need to use git reset

2018年05月23日36分29秒

See also: stackoverflow.com/a/30598953

2018年05月23日36分29秒

I think 'private'/'public' would more correctly be 'local'/'remote'.

2018年05月23日36分29秒

I'm in the 2nd case, but when I do "git revert HEAD" it says "error: Commit [ID] is a merge but no -m option was given. fatal: revert failed". Any suggestions?

1970年01月01日00分03秒

Probably worth mentioning that instead of HEAD~1 you could use the actual hash as displayed by git log --stat or by git reflog - useful when you need to 'undo' more than one commit.

2018年05月23日36分29秒

Should be marked as answer, because you can specify which SHA you want.

2018年05月23日36分29秒

What if you do not use "<commit_id>" and simply use "git reset --hard"? I typically just want to get rid of my latest updates that I have not committed yet and got back to the latest commit I made, and I always use "git reset --hard".

2018年05月23日36分29秒

JaimeMontoya To undo latest changes you can use git reset --hard , but if you have to hard remove last "n" commits you specify a SHA

2018年05月23日36分29秒

Really like -f. This is what other answers lack

2018年05月23日36分29秒

I needed to write git reset --soft "HEAD^" with double quotes, because I write it from Windows command prompt.

2018年05月23日36分29秒

It should work without any quotes.

2018年05月23日36分29秒

Maybe you could at a note/warning that his command will throw away the commit and the changes in the working directory without asking any further.

2018年05月23日36分29秒

If you happen to do this by accident, not all is lost, though. See stackoverflow.com/questions/10099258/…, stackoverflow.com/questions/15479501/… and stackoverflow.com/questions/7374069/undo-git-reset-hard/7376959.

2018年05月24日36分29秒

Use --soft to keep your changes as uncommitted changes, --hard to nuke the commit completely and revert back by one. Remember to do such operations only on changes, that are not pushed yet.

2018年05月23日36分29秒

Zaz: You are right; maybe I should have clarified that. Only files/changes that have been either added to index (/staged) or have been committed can possibly be recovered. Uncommitted, unstaged changes are, as you said, completely thrown away by git reset --hard.

2018年05月23日36分29秒

As a sidenote: Everytime a file is staged, git stores its contents in its object database. The stored contents are only removed when garbage collection is executed. It is therefore possible to recover the last staged version of a file that was not currently staged when git reset --hard was executed (see the posts linked above for more information).

2018年05月23日36分29秒