How would I "rollback" the last commit in git without deleting any changes?
This is something I've done often in hg:
- Commit "Fixed 107."
- Remembered that I forgot to do something
- hg rollback
- Do something
- Commit "Fixed 107."
Try this:
git reset --soft HEAD^
I found it to be the same of 'hg rollback', because:
You can also create a rollback
git alias like this:
git config --global alias.rollback 'reset --soft HEAD^'
So, you can now just type git rollback
to have exactly the same command that you have on Mercurial.
With git you may actually prefer to use the --amend
option in that case.
git commit --amend
and edit the notesIf you need to rollback for other reasons take a look at git revert
revert
is a way to deal with only one of several other reasons. If the commit in question as been published, then indeed, use git revert
. If it hasn't been pushed, and it's the most recent commit, use git reset [--hard] HEAD^
. If it hasn't been pushed, and it's not the most recent commit, use git rebase -i
to remove it from the history, leaving the rest intact.
git reset HEAD^
shouldn't do that, it should only modify the files in your working directory if you also use the --hard
flag.
Jul 3, 2012 at 4:06
In this specific instance I would git commit --amend
. If you haven't pushed yet and you've already committed other changes then you can also use git rebase -i
to edit whichever commit you want.
I was trying to do an hg rollback in my git repository and I managed using
git reset HEAD~
this left me with my changes in the working directory but now unstaged. Source