Git
How to Revert a Commit in GitHub?
Reverting a commit is a common task in Git and GitHub, whether to fix mistakes, undo changes, or maintain a clean history in your repository. GitHub provides straightforward methods to revert commits, either using the Git command line or GitHub’s web interface.
This blog explains how to revert a commit safely and effectively, ensuring that your repository remains in good shape.
What Does Reverting a Commit Mean?
Reverting a commit creates a new commit that undoes the changes introduced by the specified commit. Unlike resetting, which alters the repository’s history, reverting is a non-destructive operation that preserves the commit history, making it ideal for shared repositories.
Methods to Revert a Commit
1. Using the GitHub Web Interface
Step 1: Open the Repository
Navigate to the repository on GitHub where you want to revert the commit.
Step 2: Locate the Commit
- Go to the “Commits” tab under the “Code” section.
- Find the commit you want to revert.
Step 3: Revert the Commit
- Click the commit hash to view the commit details.
- Click the “Revert” button on the right side of the page.
- GitHub will prompt you to create a new commit that reverts the changes.
- Add a commit message (optional) and confirm by clicking “Commit changes”.
The changes introduced by the reverted commit will now be undone in the branch.
2. Using the Git Command Line
Step 1: Navigate to the Repository
Open your terminal and navigate to your Git repository:
cd /path/to/your/repository
Step 2: Identify the Commit
Use the git log
command to find the commit hash you want to revert:
git log --oneline
Example Output:
f4d3b2a Fix login issue
e8a7c9d Add README updates
b2c9a1d Initial commit
Copy the hash of the commit you want to revert (e.g., e8a7c9d
).
Step 3: Revert the Commit
Run the following command to revert the commit:
git revert <commit-hash>
For example:
git revert e8a7c9d
- Git will open the default text editor to edit the commit message.
- Save and close the editor to complete the revert.
Step 4: Push the Changes
Push the changes to the remote repository:
git push origin <branch-name>
Reverting a Range of Commits
If you need to revert multiple commits, use the --no-commit
flag to batch the changes and commit them together:
git revert <oldest-commit-hash>..<newest-commit-hash> --no-commit
git commit -m "Revert multiple commits"
git push origin <branch-name>
Undoing a Revert
If you mistakenly revert a commit, you can undo the revert by reverting the new revert commit. This creates another commit that restores the original changes.
Find the revert commit hash:
git log --oneline
Revert it:
git revert <revert-commit-hash>
git push origin <branch-name>
Best Practices When Reverting Commits
- Use Descriptive Messages: Clearly describe the reason for reverting a commit in the commit message.
- Communicate with Your Team: Inform your team about the revert to avoid confusion, especially in collaborative projects.
- Revert on the Correct Branch: Ensure you are on the right branch before reverting a commit.
- Test Before Pushing: Always test the changes locally before pushing to a shared repository.
Troubleshooting
Merge Conflicts When Reverting
If the revert causes conflicts, Git will pause the process and allow you to resolve the conflicts manually.
- Resolve the conflicts in the affected files.
- Add the resolved files:
git add <file>
- Complete the revert:
git commit
Accidentally Reverting the Wrong Commit
Follow the “Undoing a Revert” section above to restore the reverted changes.
Conclusion
Reverting a commit is a safe and reversible way to undo changes in Git and GitHub. Whether you use the GitHub web interface or the Git command line, following the steps outlined in this guide ensures that you can manage your repository effectively and maintain a clear history.
By adhering to best practices and testing thoroughly, you can handle revert operations confidently in any Git workflow.