If you’ve merged a pull request (PR) in GitHub and need to undo those changes, there are several ways to revert the merge. This article walks you through the process step-by-step, whether you’re working directly in GitHub or handling the changes locally.
1. Reverting the Merge Using GitHub’s Interface
This is the simplest method to revert a merged pull request directly on GitHub.
Steps:
-
Navigate to the Pull Request:
- Go to the repository on GitHub and click on the Pull Requests tab.
- Locate the merged pull request you want to revert.
-
Click the Revert Button:
- Open the merged pull request.
- Look for the Revert button on the pull request page.
- Clicking this will automatically create a new pull request that undoes the changes introduced by the merged PR.
-
Review and Merge the Revert PR:
- Review the newly created pull request for reverting changes.
- Merge this pull request into the base branch to complete the revert.
2. Reverting the Merge Locally
For more control or if you prefer using the terminal, you can revert the merge locally and push the changes back to GitHub.
Steps:
- Check Out the Branch: Switch to the branch where the merge occurred:
git checkout main
- Find the Commit Hash of the Merge: Use the following command to list the recent commits and find the merge commit hash:
git log --oneline
Look for the commit message associated with the merged pull request (e.g., "Merge pull request #123").
- Revert the Merge: Use the commit hash to revert the merge:
git revert -m 1 <merge-commit-hash>
- Replace
<merge-commit-hash>
with the actual hash of the merge commit. - The
-m 1
flag tells Git to keep the main branch’s changes (the first parent).
- Push the Changes: After resolving any conflicts and completing the revert, push the changes to the remote repository:
git push origin main
3. Deleting the Merge Commit
If you want to completely reset the branch to its state before the merge, you can use this method. However, it’s more drastic and should be used cautiously.
Steps:
- Find the Commit Before the Merge: Identify the commit hash before the merge using:
git log --oneline
- Reset the Branch: Reset the branch to the desired commit:
git reset --hard <commit-hash>
- Force Push the Changes: Overwrite the remote branch with the reset state by force-pushing:
git push origin main --force
Warning: Force-pushing can disrupt other contributors’ work. Use this method only when absolutely necessary and communicate with your team beforehand.
Best Practices for Managing Merges
- Use the Revert Button on GitHub for public repositories or collaborative projects to ensure a clean and traceable history.
- Communicate with Your Team: Always inform your team before reverting significant changes, especially if others are actively working on the same branch.
- Avoid Force-Pushing: Force-pushing can cause problems for others working on the same branch. Use it only as a last resort.
By following these steps, you can safely and effectively revert a merged pull request in GitHub. Choose the method that best fits your workflow and team setup.
Let me know if you have questions or need further assistance!
Happy coding!!
Top comments (0)