Tortoisegit undo last commit into the repo

user2742122 picture user2742122 · Oct 2, 2013 · Viewed 57.1k times · Source

Is there an option in tortoisegit to undo the last commit into the repo?

By mistake I pushed a large number of unnecessary files into my git repository(branch:master) which I need to undo. I have searched a lot for the right option in tortoise git to undo the push and go back to the state before the last commit. Please show me a way to undo my last commit.

Answer

rustyx picture rustyx · Mar 26, 2014

If you haven't pushed your changes yet (so your commit is only local)

  1. TortoiseGit -> Show log
  2. Select the commit to which you want to rollback to
  3. Reset "<branch>" to this...

If you have, then this can still be done, but then you'd have to also do a force-push (check "overwrite known changes" 1).

1 The "overwrite known changes" flag will replace the already pushed commit with a new one. If there is a chance that someone already fetched the commit that you're replacing, don't use this feature, otherwise doing so will create a fork in the history with two conflicting truths.


There is also a shortcut for when you want to just (1) redo the last commit and (2) you haven't pushed it yet:

  1. Commit -> Check "Amend Last Commit"

That will replace the last commit with a new one. But I don't recommend using this - if the last commit is already pushed, you can end up with a big mess. TortoiseGit will not stop you here.

By doing a Reset you are forced to have a look at the log, and there you see if the commit is local or not.