Git
How to Revert a Commit in GitHub?
When working with GitHub, it’s not uncommon to make a mistake or realize that a specific commit introduced a bug or unwanted changes. Fortunately, GitHub and Git offer a way to undo changes safely using the revert command. Reverting a commit is a clean and straightforward way to undo changes without rewriting history, making it a preferred method for collaborative projects.
In this blog, we will walk you through how to revert a commit in GitHub, both via the Git command line and directly through the GitHub web interface.
What Does Reverting a Commit Mean?
Reverting a commit doesn’t delete or rewrite the repository’s history. Instead, it creates a new commit that undoes the changes introduced by a specific commit.
Key Benefits of Reverting a Commit:
- Safe for Collaboration: It preserves the integrity of the repository history, avoiding conflicts for collaborators.
- Traceable: The revert commit is recorded in the history, providing a clear audit trail of changes.
- Selective: You can target specific commits without affecting others.
When Should You Revert a Commit?
You might want to revert a commit in scenarios such as:
- A bug was introduced in a recent commit.
- Unnecessary files were accidentally committed.
- Specific changes need to be undone without altering subsequent changes.
How to Revert a Commit in GitHub
1. Using Git Command Line
Step 1: Identify the Commit to Revert
First, locate the commit hash (also known as the SHA) of the commit you want to revert.
To see the commit history, use:
git log --oneline
Example output:
d4c3b2f Update README.md
a9f8e7c Add feature implementation
c3d5e4a Fix calculation function
Copy the hash of the commit you want to revert (e.g., a9f8e7c
).
Step 2: Revert the Commit
To revert the changes introduced by the commit, run:
git revert <commit-hash>
Example:
git revert a9f8e7c
This creates a new commit that undoes the changes introduced by the specified commit.
Step 3: Resolve Conflicts (If Any)
If the revert causes conflicts, Git will pause and prompt you to resolve them.
- Open the affected files and resolve the conflicts.
- Once resolved, stage the changes:
git add <file>
- Complete the revert:
git revert --continue
Step 4: Push the Changes
After successfully reverting the commit, push the changes to the remote repository:
git push origin <branch-name>
Example:
git push origin main
2. Using the GitHub Web Interface
Step 1: Navigate to the Commit
- Go to your repository on GitHub.
- Click on the Commits link, typically found in the repository’s main branch.
Step 2: Select the Commit to Revert
- Identify and click on the commit you want to revert.
- On the commit page, click the Revert button.
Step 3: Confirm the Revert
- GitHub will create a new commit that reverts the changes introduced by the selected commit.
- Review the proposed changes.
- Click Create a Pull Request if you are working on a protected branch, or Commit Changes if you have write access to the branch.
Step 4: Merge or Push the Revert Commit
If you created a pull request, merge it to complete the revert process.
Key Considerations When Reverting a Commit
- Reverting a Merge Commit: Reverting a merge commit requires additional options to specify which parent branch to revert. Use:
git revert -m 1 <merge-commit-hash>
Replace 1
with the appropriate parent branch number.
- Avoid Reverting Reverts: Be cautious to avoid creating a revert of a revert commit, as this can reintroduce changes unintentionally.
- Collaborative Repositories: If you’re working on a shared repository, ensure you communicate with your team before reverting a commit to avoid confusion or conflicts.
Best Practices for Reverting Commits
- Test Locally First: Before pushing a revert, test the changes locally to ensure they resolve the issue without causing new problems.
- Write Clear Commit Messages: Git automatically adds a message like “Revert ‘Commit message'”, but you can customize it to provide more context.
- Avoid Force Pushing: Reverting is safe and does not require rewriting history, unlike a reset, which may need a force push.
- Revert Commits Incrementally: Revert one commit at a time to keep the history clear and manageable.
Conclusion
Reverting a commit is a clean and collaborative-friendly way to undo changes in GitHub. Whether you’re using the Git command line or GitHub’s web interface, the process is straightforward and preserves the repository’s history.
By understanding how to revert commits and applying best practices, you can maintain a clean and organized codebase, ensuring that mistakes are corrected efficiently without disrupting your workflow.