Sometimes, after pushing changes, you may need to undo these changes due to errors, or other unintended consequences. This guide will explore how to effectively revert a commit after it has been pushed to a remote repository, covering several methods and best practices.
Understanding git revert
"Reverting" in Git refers to creating a new commit that undoes the changes made by previous commits. This is different from "deleting" or "removing" a commit from the repository's history which makes it look like the commit never existed in the first place. Reverting retains the context of the commit and why it was removed.
How to revert a commit in git after already pushing the commit
Here’s a step-by-step guide on how to revert a pushed commit without rewriting the project history.
Step 1: Identify the commit to revert
First, you need to identify the commit you want to revert. You can view the commit history by running:
git log --oneline
This command shows a simplified commit log. Find the commit hash of the commit you wish to revert (the unique alphanumeric identifier generated by the SHA-1 hashing algorithm), and copy it.
Step 2: Revert the commit
Once you have the commit hash, you can revert it using:
git revert <commit-hash>
This command creates a new commit that undoes the changes made by the commit specified by <commit-hash>
. If the commit in question introduced changes across multiple files, Git might prompt you to confirm the revert process for each conflicted file.
Step 3: Resolve any conflicts
If the revert process introduces conflicts (because the changes in the commit being reverted interact with changes made in subsequent commits), Git will pause the operation and allow you to manually resolve these conflicts. Once you have resolved any conflicts:
- Add the resolved files to the staging area using:Terminalgit add <file-name>
- Complete the revert process by running:Terminalgit revert --continue
For a detailed guide on resolving merge conflicts, see this guide.
Step 4: Push the revert commit
After successfully reverting the commit locally, push the changes to the remote repository:
git push origin <branch-name>
This updates the remote branch with the new commit that effectively undoes the changes made by the previous commit.
Alternative methods
Reverting multiple commits
If you need to revert more than one commit, you can use the git revert
to specify a range of commits:
git revert --no-commit <newest-commit-hash>^..<oldest-commit-hash>git commit -m "Revert multiple commits"
This command sequence reverts all changes in the specified range, combining them into a single commit.
Best Practices
- Communicate with your team: Before reverting commits that have been pushed, especially in shared repositories, inform your team about the changes.
- Avoid rewriting public history: Prefer
git revert
over methods likegit reset
for public branches to avoid creating synchronization issues for others.
For further reading on reverting commits in Git see the official Git documentation.