Skip to main content
Git Undo Last Commit
4 min read

Git Undo Last Commit

Introduction

Have you ever committed changes in Git, only to suddenly realize that you made a mistake? You are not alone! A great number of developers have faced at least one point in time when they needed to undo the last commit. Fortunately, Git gives us an open door to backpedal on commits and can still retain the changed files in our working directory. In this article, I'll show you how to take a step through this process and, at the end, still be able to tidy up your commit history without losing your hard-earned work.

What Happens During a Git Commit?

The first thing to do is understand what happens in Git when one commits. A commit documents changes to a repository, and it saves a snapshot of a project. Included in the snapshot is the state of the files in the staging area, and a unique identifier, too-what's called the commit hash. When we commit, our changes are saved into the history of versions that help to track modifications throughout time. From now on, in order that you understand this, I am going to add some typical commit commands along with outputs for clarity.

A normal command for committing looks something like this:

# Example of making a commit
git commit -m "Initial commit"
# Output
[master (root-commit) 1a2b3c4] Initial commit

This command tells Git that you are ready to save the current state of your changes into the repository. The output confirms that a new commit has been created; you may notice that it also states the commit hash, which acts as a UNIQUE IDENTIFIER for this particular state of your project. That sets the stage for later understanding why we might need to undo a commit.

Reverting Your Last Commit

Now, we will go through a quick recap of how to undo our last Git commit, keeping our changes in the working directory intact. For that, we execute the command git reset --soft HEAD~ where this command moves the HEAD pointer one commit back and puts all of your changes left in the staging area. That can be done by:

# Undo the last commit but keep changes
git reset --soft HEAD~
# Output
# This moves HEAD back to the previous commit while keeping changes.

This above command will leave your files in their state before the last commit, so that you can edit any changes you want. This is very helpful if it was at this stage you realized that you had forgotten to include an important change or perhaps you messed up the commit message. Now, edit your files, add new changes, and commit again at your convenience!

Amending Your Last Commit

Instead of reverting from it, you could just amend your last commit. The git commit --amend command allows you to add your new changes to the previous commit. This is great when you want to make more changes, but don't want to pollute your commit history. Here's how you do it:

# First, stage your changes
git add .
# Then amend the last commit using:
git commit --amend
# Output
[master 1a2b3c4] Updated commit message

By using this method, you can refine your changes before finalizing them. When you run the amend command, you can also modify the commit message if necessary. This approach can help you keep your commit history cleaner and more organized, ensuring that all relevant changes are captured in a single commit.

Common Pitfalls to Avoid

As always, there are a few common gotchas to watch out for when reverting your commits. First, make sure you're reverting the correct commit by checking your commit history with git log.

# Check your commit history
git log
# Output
commit 1a2b3c4
Author: You <[email protected]>
Date: Mon Jun 1 12:34:56 2021.

Also be very deliberate about commands like git reset --hard, as these delete changes permanently. If you try to revise a pushed commit by mistake, note that running git push --force creates a lot of problems for people working with you.

Conclusions

All in all, taking back your last commit with the preservation of changes is a plus for any developer. You modify your last commit with commands like git reset --soft HEAD~ or continue working without losing the changes. Always remember to check your commit history before proceeding and be tactful with your pushed commits. With this, you will try to enhance both your efficiency and confidence in using Git.