How to Revert a Git Commit Effectively
Q: Use Git to revert a commit, either by resetting to a previous commit or by creating a new commit that undoes the changes.
- Git
- Mid level question
Explore all the latest Git interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Git interview for FREE!
Yes, Git provides multiple ways to revert a commit. One way is to reset to a previous commit, and another way is to create a new commit that undoes the changes introduced by the original commit. Here are the steps for each approach:
Resetting to a previous commit:
1. Find the commit hash of the commit you want to revert to using git log. For example, let's say the commit hash is abcdefg.
2. Use the git reset command to move your current branch back to the commit you want to revert to:
git reset --hard abcdefg
This will delete any changes made after the selected commit and move your branch to that commit.
3. Force push the changes to the remote repository:
git push --force
Creating a new commit that undoes changes:
1. Find the commit hash of the commit you want to revert using git log. For example, let's say the commit hash is abcdefg.
2. Use the git revert command to create a new commit that undoes the changes introduced by the selected commit:
git revert abcdefg
This will open a text editor to enter a commit message for the new commit.
3. Save and close the text editor to create the new commit.
4. Push the changes to the remote repository:
git push
Note that if the commit you want to revert is a merge commit, you may need to use additional options with git revert to specify which parent commit to revert.


