I did a git commit
but I have not pushed it to the repository yet. So when I do git status
, I get '# Your branch is ahead of 'master' by 1 commit.
So if I want to roll back my top commit, can I just do:
git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316
given that when I do git log
I get:
commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316 Date: Tue Sep 29 11:21:41 2009 -0700 commit db0c078d5286b837532ff5e276dcf91885df2296 Date: Tue Sep 22 10:31:37 2009 -0700
Forcefully Change Historygit rebase -i HEAD~<number of commits to go back> , so git rebase -i HEAD~5 if you want to see the last five commits. Then in the text editor change the word pick to drop next to every commit you would like to remove. Save and quit the editor. Voila!
The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.
IF you have NOT pushed your changes to remote
git reset HEAD~1
Check if the working copy is clean by git status
.
ELSE you have pushed your changes to remote
git revert HEAD
This command will revert/remove the local commits/change and then you can push
Actually, when you use git reset
, you should refer to the commit that you are resetting to; so you would want the db0c078
commit, probably.
An easier version would be git reset --hard HEAD^
, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.
Beware when you do any git reset --hard
, as you can lose any uncommitted changes you have. You might want to check git status
to make sure your working copy is clean, or that you do want to blow away any changes that are there.
In addition, instead of HEAD you can use origin/master
as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With