How to Undo the Last Git Commit in a Local Repository

John Wachira Feb 15, 2024
How to Undo the Last Git Commit in a Local Repository

This article will discuss how we can undo the latest commit in Git. This comes in handy when we want to remove the changes introduced by a commit before pushing it to the remote repository.

Let’s jump right in.

Undo the Last Commit Without Modifying Files in Git

We will kick off things by discussing how we can undo a commit while the previous modifications are made to the files. To do this, we use the git reset command with the -soft argument to instruct Git to preserve the contents of our file.

$ git reset --soft HEAD~1

The command above will remove the last commit on our HEAD.

Let’s look at an example. Here is the commit history in our local repository.

$ git log --oneline --graph
* c5bf6c8  (HEAD -> master) Added a new file named "file.txt"
* 3b641e0  Second commit
* 21ca1e7  Initial commit

Lets us run the git reset command and check its impact on our local repo.

git reset

The above output shows that our file.txt is in our index, but the commit is not present. We can make modifications to the file and commit it again.

$ git log --oneline --graph
* c5bf6c8  (HEAD -> master) (HEAD)
* 3b641e0  Second commit (HEAD~1)
* 21ca1e7  Initial commit (HEAD~2)

Our command $ git reset --soft HEAD~1 removes the last commit. If we were to remove the last two commits, we would have instead used $ git reset --soft HEAD~2 and so on.

Sometimes, we may want to eliminate the commit and the files. We use the git reset command with the -hard option, as shown below.

$ git reset --hard HEAD~1

The command above will remove all modifications associated with the commit from our index and working directory.

Let’s look at an example. Here is a visual of our commit history.

$ git log --oneline --graph
* c5bf6c8  (HEAD -> master) Added a new file named "file.txt"
* 3b641e0  Second commit
* 21ca1e7  Initial commit

In this scenario, we would like to remove the last commit and do away with the modifications.

We run:

$ git reset --hard HEAD~1
HEAD is now at 3b641e0 Second commit

Let’s run the git status command to check the state of our repo.

git status

The above output shows that Git removed the files from our index and working directory.

We may also need to undo the last commit and retain changes in the working directory but on our index. To do this, we add the --mixed option to our git reset command, as shown below.

$ git reset --mixed HEAD~1

Let’s look at an example. This is the current state of our commit history.

$ git log --oneline --graph
* c5bf6c8  (HEAD -> master) Added a new file named "file.txt"
* 3b641e0  Second commit
* 21ca1e7  Initial commit

We run:

$ git reset --mixed HEAD~1

Our files should be visible in the working directory but absent in the index. Let’s confirm this with the git status command.

confirm with git status

We can see that our file.txt is under untracked files. This is another way of undoing a commit while retaining modifications.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Commit