
How to Revert Last Commit in Git
Learn the step-by-step process to undo your last Git commit, including methods for local and remote changes.

Yogini Bende
Sep 15, 2024 • 4 min read
TL;DR
How to Revert Last Commit in Git locally and remotely
git reset HEAD~1
git revert HEAD
Git is an essential tool for version control in software development. However, mistakes happen, and you might find yourself needing to undo your last commit. This comprehensive guide will walk you through various methods to revert the last commit in Git, ensuring you can maintain a clean and efficient workflow.
Table of Contents
Understanding Git Commits
Before we dive into reverting commits, it's crucial to understand what a Git commit represents. A commit in Git is a snapshot of your project at a specific point in time. It includes all the changes made to your files since the last commit, along with metadata such as the author, date, and a commit message.
Why You Might Need to Revert a Commit
There are several reasons why you might need to undo your last commit:
Accidental commits with incomplete or incorrect changes
Committing to the wrong branch
Realizing a bug was introduced in the last commit
Needing to add or remove files from the last commit
Regardless of the reason, Git provides multiple ways to address these situations.
Method 1: Using git reset
The git reset
command is a powerful tool for undoing changes in your local repository. Here's how to use it to revert the last commit:
git reset HEAD~1
This command moves the HEAD and branch pointer back by one commit, effectively "uncommitting" the last change. There are three main options for git reset
:
--soft
: Keeps your changes staged--mixed
(default): Unstages your changes but keeps them in your working directory--hard
: Completely discards the changes
Example:
# To keep changes staged
git reset --soft HEAD~1
# To unstage changes (default behavior)
git reset HEAD~1
# To discard changes completely (be cautious!)
git reset --hard HEAD~1
Note: Use git reset --hard
with caution, as it permanently discards your changes.
Method 2: Using git revert
While git reset
is great for local changes, git revert
is preferable when you've already pushed your commits to a shared repository. It creates a new commit that undoes the changes from the previous commit:
git revert HEAD
This command opens your default text editor to enter a commit message for the revert operation. After saving and closing the editor, a new commit is created that reverses the changes from the last commit.
Method 3: Amending the Last Commit
If you need to modify the last commit (e.g., to add forgotten files or change the commit message), you can use the --amend
option:
# To change the commit message
git commit --amend -m "New commit message"
# To add forgotten files
git add forgotten-file.txt
git commit --amend --no-edit
The --no-edit
flag keeps the original commit message.
Reverting Pushed Commits
If you've already pushed your commit to a remote repository, it's generally better to use git revert
instead of git reset
. This is because git revert
creates a new commit that undoes the changes, preserving the history and avoiding conflicts for other collaborators.
git revert HEAD
git push origin <branch-name>
Best Practices for Git Workflow
To minimize the need for reverting commits, consider these best practices:
Use meaningful commit messages
Commit frequently with smaller, logical changes
Review your changes before committing (use
git diff
andgit status
)Use feature branches for new development
Utilize Git hooks for pre-commit checks
Troubleshooting Common Issues
Merge Conflicts: If you encounter merge conflicts when reverting, resolve them manually and complete the revert process.
Losing Important Changes: Always create a backup branch before performing major operations:
git branch backup-branch
Reverting Multiple Commits: To revert multiple commits, specify a range:
git revert HEAD~3..HEAD
Undo a Revert: If you mistakenly revert a commit, you can revert the revert:
git revert <revert-commit-hash>
Conclusion
Mastering the art of reverting commits in Git is crucial for maintaining a clean and efficient development workflow. Whether you're working alone or in a team, understanding these techniques will help you confidently manage your version control and recover from mistakes.
And if you are looking for a Entry Level Software Developer Jobs, you will find many good opportunities on Peerlist Jobs. Don't forget to check them out.
Remember, the key to successful Git usage is practice and consistency. Don't be afraid to experiment in a safe environment to become more comfortable with these commands. Happy coding!