How to Undo a Last commit – git undo commit

Git is one of the most popular version control system and a powerful tool used by developers. It offers a variety of features and functionalities that are valuable to developers, teams, and anyone managing projects. Git commit is a fundamental feature of them which represents a single, logical change. Sometimes you need to undo a commit or revert changes when you accidentally committed the wrong files to Git, but didn’t push the commit to the server yet or you commit an undesirable change to a repository. Fortunately, Git provides various methods such as “git reset”, git revert, “git restore”, and git cherry-pick to undo commits based on specific needs. Let’s understand how to undo the changes in git depending upon the state of git repository.

How to undo a Git commit?

Before diving into the methods to undo commits, the next few steps set up a new example Git repository. Remember that each commit has a unique identifier (a hash) and it includes changes made to files since the last commit.

Step 1 : Create a new directory for your Git Repository. Here the new directory is ‘git-example’.

mkdir ~/git-example
cd ~/git-example

Step 2 : Run the following command to initialize the new Git repository:

git init

Step 3 : Create new empty files using the touch command.

touch example-file-1.txt
touch example-file-2.txt

Step 4 : Add the files in stage area, then proceed to commit the changes.

git add .
git commit -m "Initialized repo."

Step 5 : Now make changes and add content to the files and then commit.

for first file

echo "Some example text for the first file." >> example-file-1.txt
git add example-file-1.txt
git commit -m "Added text to first file."

for second file

echo "Some example text for the second file." >> example-file-2.txt
git add example-file-2.txt
git commit -m "Added text to second file." 

Step 6 : Now, your Git repository contains a few files and multiple commits, all of which you can view listed by:

git log --oneline

f4391b2 (HEAD -> master) Added text to second file.
e3c534a Added text to first file.
0b24777 Initialized repo.

In the above top line the “f4391b2” ID, signifies the most recent commit.

By using the Last commit Id you can undo that commit using two most common methods, which are as follows:

Undo a commit using git revert

The “git revert” command undoes the changes we have made recently by creating a new commit. It does not modify the history that’s why it’s a safe way.

git revert f4391b2

After running the command, Git starts a new commit to revert the changes.

[master e86542a] Revert "Added text to second file."
1 file changed, 1 deletion(-)

Now use the below ‘log’ command to display the commit history.

git log --oneline

e86542a (HEAD -> master) Revert “Added text to second file.”
f4391b2 Added text to second file.
e3c534a Added text to first file.
0b24777 Initialized repo.

If you want to verify whether the changes have been reversed, you can check the modified file. here we have made the changes in example-file-2.txt.

cat example-file-2.txt

Undo a commit using git reset

The “git reset” command is also used to undo the changes made by a commit. It manipulates the commit history by resetting the current branch to a specific commit. However, it is advised to be cautious before running the command as it permanently erases the commit history.
There are three primary modes of git reset:

  • The soft reset mode moves the HEAD pointer to the specified commit while preserving file changes in the working directory and staging area.
git reset --soft HEAD~1
  • The mixed reset mode moves the HEAD pointer to the specified commit while keeping file changes in the working directory, but these changes remain unstaged. This is the default mode of reset command.
git reset --mixed HEAD~1
  • The hard reset mode moves the HEAD pointer to the specified commit by discarding all the changes made after that commit.
git reset --hard HEAD~1

The log now displays the results of the reset process.

git log --oneline

Here you will see that the last commit has been removed from the commit history.

e3c534a (HEAD -> master) Added text to first file.
0b24777 Initialized repo.

Also, you can undo the changes through ‘git cherry-pick’ command. Using this command, you can choose to apply the changes of the specified commit to the current branch in a way that effectively reverses those changes.