How to Undo in Git using SourceTree

If you are not a command-line person, working with Git could be a painful exercise. You can use SourceTree to make your Git workflow a lot simpler. Recently, I encountered a few folks who were not happy with the easy & traditional undo options in Git. This article will try to address various cases where you would want to undo and rewrite history. You can simply follow through, but it would be best if you try it out yourself to get a proper hang of it. Let's get started.

Step 1: Setup a repository for test

Go to Bitbucket and click on the + icon to create a new repository.

setup a repository

Step 2: Clone it locally

Open SourceTree and switch to Remote. You can filter using the search bar to find your repository. Click on the Clone option.

switch to remote and find your repo

clone

Step 3: Commit a file

By default, with SourceTree you will commit a file and push the changes immediately. Notice the check box just below the commit message.

first commit

You can view the history and if everything is good, you will see the messages along with all the commits.

history

Case 1: How to fix the last commit message?

To demonstrate this case, I have modified the 1.txt file and intentionally introduced a typo in the commit message.

introduce a typo

You can fix this error message, clicking on the Commit button followed by Commit Options > Amend Last Commit option.

amend last commit

Fix the comment message and commit.

fix and commit

Wait, what? An error message?? Why did that happen???

error message

push error

If you read it carefully, you will find that it gives you a hint about pulling from remote and pushing again. Let's do that! Pull it down.

pull

AND, push! The history clarifies what went on behind the scenes.

push

How to fix the ugliness though? It would have been great if there was no tree and merge here, right? So, basically this happened due to the fact that we are pushing the changes instantly.

Fix: Don't push instantly to remote!

Make some more changes and commit. Notice that the check box Push changes immediately to origin/master is NOT checked.

commit without pushing

Now, check the history. You will find the local master 1 ahead. This is good. You get the goodness of git locally, without changing the remote yet. Also notice the typo in the commit. It reads linee instead of line.

local master ahead

Try Amend last commit again. This time, you will find that the message appropriately changes without creating a split in the tree structure!

Amend last commit again

You can set up your preferences in SourceTree so that it doesn't push automatically.

source tree preference

Case 2: Undo changes by Soft Reset

So, let's add a line to the 1.txt followed by a commit. Repeat this exercise three times, so that you have a history similar to the following:

few more commits

The requirement here is to reset to the commit where we Added line 6 and NOT lose the changes after that (like Line 7 & 8). In this case, you should do a Soft Reset. Right click and choose Reset master to this commit.

reset to 6

In the option that appears, select Soft - keep all local changes

soft reset

Once done, you can check the result. Notice the Uncommitted changes. So, basically all lines are still intact and you are good to go. Also note, that 1.txt is still staged. soft reset result

Case 3: Undo changes by Hard Reset

It is quite similar to Reset when it comes to the way it handles history. However, it discards all the local changes. Hence you should use it with caution.

hard reset

Case 4: Undo changes by Mixed Reset

This is again similar to Reset when it comes to the way it handles history. However, it unstages the files too while keep the changes. Note that 1.txt is now unstaged.

mixed reset

Case 5: Undo using Revert

When you use Revert, basically you don't rewrite the history. You actually, revert the changes using a new commit. Check the example below.

Original history

Reverting Line 6

Reverting it...

Revert

And now you have...

Reverted

Case 6: Reset vs Rebase

Jarrod Spillers has explained it beautifully here. TL;DR > Don’t rebase branches you have shared with another developer.

Hope this helps! Happy Coding :-)

What next?

Stay tuned for upcoming articles. You may contact us for your software and consultancy requirements.

© 2024, Attosol Private Ltd. All Rights Reserved.