简体   繁体   中英

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:

  • 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:

  • 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM