MOHAMED
MOHAMED

Reputation: 43616

Undo git commit with TortoiseGit

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

Upvotes: 10

Views: 15937

Answers (2)

Yue Lin Ho
Yue Lin Ho

Reputation: 3121

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

Upvotes: 17

linquize
linquize

Reputation: 20396

TortoiseGit -> Show Log -> Revert change by this commit

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

Upvotes: 6

Related Questions