/github-for-non-tech

How to revert a merge in GitHub?

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.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to revert a merge in GitHub?

 

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.

  • On GitHub: Navigate to the “Commits” history of the branch where the merge occurred. Look for the commit message that indicates a merge (it typically starts with “Merge pull request #…”).
  • In your terminal: Use 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.

  • Replace 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.

  • Open conflicted files and remove the conflict markers (<<<<<<<, =======, >>>>>>>).
  • After resolving, stage the changes:

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:

  • Go to your repository.
  • Click the “Compare & pull request” button next to your revert-merge branch.
  • Review the changes, give your PR a descriptive title (e.g., “Revert merge commit COMMIT\_SHA”), and add context in the description.
  • Submit the pull request and request reviews if needed.

 

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:

  • Checkout your main branch:

git checkout main
git pull
  • Review the commit history to confirm that the revert commit appears and that the original merge changes have been undone.
  • Test your codebase to verify no unintended side effects remain.

 

Additional Tips for GitHub Beginners

 

  • Use git status frequently to see your current state and staged files.
  • Read commit messages carefully to ensure you’re reverting the correct merge.
  • Communicate with your team before reverting large merges to avoid confusion.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022