10

I'm using TortoiseGit in Windows for my git projects.

I made a local commit on my project. And I want to undo it.

How to undo git commit with TortoiseGit? without discarding my changes

5
  • Would you mind to use the command-line git client for this operation? Commented Apr 25, 2014 at 11:07
  • @user3159253 I know how to undo with command-line. I want to undo with Tortoise. I do not want to play with 2 tools Commented Apr 25, 2014 at 13:42
  • Ah, Ok :) Personally I think that most if not all VCS GUIs suck but certainly your exprerience may differ. Commented Apr 25, 2014 at 13:44
  • What do you mean by "without discarding my changes"? The local changes? The commits you want to keep except the one you want to undo? Commented Sep 8, 2017 at 2:58
  • How do you undo with command line? Commented Sep 8, 2017 at 2:58

2 Answers 2

17

Abstract

If you don't want to discard the working tree changes, using Reset with Mixed option or commit it with another branch to keep it.

If you want to keep changes in a commit, do what you want to do except clean up the repository(garbage collection). Those changes can be retrieved later.


Detail Steps

2023/12

Win10 TortoiseGit 2.15.1.0 git version 2.43.0.windows.1 

Undo last commit: Reset with Mixed option

Condition:

  • Working Tree has some changes and you want to keep it.
  • Or, Working Tree is clean, but you want to keep local changes as same as last commit.

enter image description here

and

enter image description here

The result:
enter image description here

If pushing F5 to refresh, that last commit will be invisible.
enter image description here
I will show you how to retrieve that commit later.

Undo last commit: Reset with Hard option

If you don't care the Working Tree Changes nor the last commit's changes, perform Git Reset with Hard.

enter image description here

The result:
enter image description here

Also, that last commit will be invisible after pushing F5.
But, it can be retrieved.

Retrieve invisible commit

You could create a local branch keep to keep the commit at the beginning. Like this:
enter image description here

If not...
Since you have done the commit, that commit will exist in the near future. So you can find it back by using Reflog.

enter image description here

Then, you can show log for that invisible commit, or create a local branch for it.
enter image description here

NOTE: Those invisible commits will be accessible until the repository is cleaned up.

Undo/Remove the commit which is not the last one: Rebase with Force

enter image description here

and

enter image description here

(You may hit the conflicts, but how to resolve the conflicts is another story. :P )

enter image description here

The Result:
enter image description here

NOTE: Also, you can create keep branch at beginning or by using Reflog to retrieve it later. If so, the Graph of Show Log will be:
enter image description here

Sign up to request clarification or add additional context in comments.

12 Comments

but you will lose changes also
With Rebase function, not lose that commit "3". User said this: "But I want to remove it from the history also." Suppose user meant that he want to remove "2", and not lose "3".
he wants to remove from history, but keep local changes
No. He didn't say "Local" changes.
he said: "without discarding my changes"
|
6
TortoiseGit -> Show Log -> Revert change by this commit 

This will generate a commit that revert the commit, so you won't "lose" your changes.

3 Comments

I tried this. this will keep the old commit in the history. But I want to remove it from the history also
If you use Reset, it will lose the commit, unless you go to reflog as soon as possible (before garbage collection).
This worked for me. I use Tortoise Git client for windows version 2.4.0. The only change here is that there was no "Show Log" under TortoiseGit. There is one by the name "Show Reflog", which in turn showed all the commits. I could undo and create a new "undo commit". I am fine with the history being maintained.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.