Learn how to safely revert a merge on GitHub. This step-by-step guide shows you how to identify the merge commit, create a revert branch, resolve conflicts, and verify the changes.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Identify the merge commit to revert
First, you need to locate the exact SHA (commit ID) of the merge commit that you want to undo. You can do this either on the GitHub web interface or in your local repository.
git log
to list recent commits and find the merge commit SHA.
git log --oneline
Step 2: Create and switch to a new branch (recommended)
Before making any destructive changes, it’s best practice to create a new branch. This way, you can test the revert and push changes safely without affecting your main branch until you’re ready.
revert-merge
with any descriptive branch name.
git checkout -b revert-merge
Step 3: Revert the merge commit locally
Use git revert
with the -m
flag to revert a merge. The -m
option specifies the parent number. In most cases you’ll use 1
to keep changes from the branch you merged into.
COMMIT\_SHA
is the SHA of the merge commit you identified in Step 1.
git revert -m 1 COMMIT\_SHA
This command generates a new commit that undoes all of the changes introduced by the merge commit. If there are conflicts, Git will pause and ask you to resolve them manually.
Step 4: Resolve any conflicts
When a revert hits conflicts, Git will mark the files in conflict. You need to open each conflicted file, edit the conflict markers, and then stage the resolved files.
<<<<<<<
, =======
, >>>>>>>
).
git add path/to/conflicted-file
Once all conflicts are resolved and staged, complete the revert:
git revert --continue
Step 5: Push the revert branch to GitHub
After successfully reverting the merge locally, push your new branch to GitHub:
git push --set-upstream origin revert-merge
Step 6: Open a Pull Request to merge the revert
On GitHub:
revert-merge
branch.COMMIT\_SHA
”), and add context in the description.
Step 7: Merge the revert Pull Request
Once your revert PR is approved and passes any CI checks, merge it into your main branch using either “Merge” or “Squash and merge” (your team’s preferred strategy). This effectively undoes the original merge on the default branch.
Step 8: Verify the revert
To ensure everything is clean:
git checkout main
git pull
Additional Tips for GitHub Beginners
git status
frequently to see your current state and staged files.When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.