Skip to content

How to Revert the Last Commit in Git: A Beginner’s Guide

how to revert the last commit in git
Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

So you are wondering how to revert the last commit in git because you unintentionally pushed some changes?

Well, reverting changes in Git can sometimes feel like navigating a maze, especially for beginners. However, understanding how to undo your last commit is crucial for maintaining a clean project history and fixing mistakes before they affect others.

This short article will demystify the process, teaching you how to revert your last commit in Git in a way that’s easy to understand.

Step 1: Identifying Your Commit

Before reverting, identify the commit you wish to undo. The git log command shows a list of recent commits:

git log

Look for your commit’s unique identifier, known as the SHA-1 hash. It’s a long string of characters at the start of each commit entry.

Step 2: The Magic of git reset

To undo your last commit, you’ll use the git reset command. This command is your time machine in Git, allowing you to go back to the state before your last commit. There are a few ways to use git reset, depending on how much of your last commit you want to undo:

  • Keeping Your Changes (--soft): If you want to undo the commit but keep your changes for further work, use:
    git reset --soft HEAD~1
  • Discarding Your Changes (--hard): If you want to completely discard your changes, use:
    git reset --hard HEAD~1

Be cautious with --hard, as it will remove all changes in your working directory.

Step 3: Editing and Re-committing

If you used --soft, your changes are still in your working directory. You can edit your files, fix any mistakes, and prepare them for a new commit:

  1. Make the necessary changes to your files.
  2. Stage your changes with git add.
  3. Commit your changes with a new commit message:
    git commit -m "Your new, corrected commit message"

 

Step 4: Pushing Changes (If Needed)

If you’ve already pushed your incorrect commit to a remote repository, you’ll need to force push your changes after resetting. Use this command cautiously, as it can rewrite history in the remote repository:

git push --force

Only use force push if you’re sure no one else has pulled your changes, as it can disrupt the workflow of others.

Conclusion: How to revert the last commit in git

Mistakes are a natural part of the development process, and Git offers powerful tools to fix them. By using git reset, you can undo your last commit and keep your project history clean and accurate. Remember to use --soft if you want to keep your changes, and --hard if you don’t. And always be careful with git push --force, as it can have far-reaching effects on your project’s history. Wondering how to stash changes using git? Read our short guide on that here.

Happy coding, and may your Git journey be smooth and error-free!

Oh, and if you feel overwhelmed with coding, check out our developer membership (seriously, it’s worth it!). We help you master coding fast and easily.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!