user2742122
user2742122

Reputation: 623

Tortoisegit undo last commit into the repo

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.

Upvotes: 48

Views: 70834

Answers (2)

rustyx
rustyx

Reputation: 85361

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... (Note: The commit is not lost after the reset, the HEAD is just not pointing to it)
  4. TortoiseGit -> Show log

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.

Upvotes: 65

Endre Simo
Endre Simo

Reputation: 11551

If you need to revert back to the previous state before the last commit just select the commited action from the log list and select revert changes by this commit.

Take care, you need to commit and push again the changes made.

Upvotes: 42

Related Questions