Nick Zalutskiy
Nick Zalutskiy

Reputation: 15440

git equivalent for hg rollback

How would I "rollback" the last commit in git without deleting any changes?

This is something I've done often in hg:

Upvotes: 23

Views: 3664

Answers (4)

iacopo
iacopo

Reputation: 683

Try this:

git reset --soft HEAD^

I found it to be the same of 'hg rollback', because:

  • last commit cancelled
  • changes preserved
  • files previously committed are staged

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.

Upvotes: 22

Felipe
Felipe

Reputation: 440

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

Upvotes: 1

chmullig
chmullig

Reputation: 13406

With git you may actually prefer to use the --amend option in that case.

  • Commit "Fixed 107."
  • Remembered that I forgot to do something
  • Do something
  • git commit --amend and edit the notes

If you need to rollback for other reasons take a look at git revert

Upvotes: 20

Nathan Kidd
Nathan Kidd

Reputation: 2959

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.

Upvotes: 3

Related Questions