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?
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?
I wanted to undo the lastest 5 commits in our shared repository. I looked up the revision id that I wanted to rollback to. Then I typed in the following.
or
Warning: The above command will permanently remove the modifications to the
.java
files (and any other files) that you wanted to commit.The
hard reset
toHEAD-1
will set your working copy to the state of the commit before your wrong commit.If you have Git Extras installed, you can run
git undo
to undo the latest commit.git undo 3
will undo the last 3 commits.Use reflog to find a correct state
Select the correct reflog (f3cb6e2 in my case) and type
After that the repo HEAD will be reset to that HEADid
LOG AFTER RESET
Finally the reflog looks like the picture below
I prefer to use
git rebase -i
for this job, because a nice list pops up where I can choose the commits to get rid of. It might not be as direct as some other answers here, but it just feels right.Choose how many commits you want to list, then invoke like this (to enlist last three)
Sample list
Then Git will remove commits for any line that you remove.
Use
git revert <commit-id>
To get the commit ID, just use
git log