Skip to content

Revert Merge in Git

An illustration for a blog post titled 'Revert Merge in Git' featuring a symbolic representation of a Git merge process with two branches coming together into a single line, then splitting back into two separate lines to symbolize the revert action. The branches are depicted in a digital, schematic style, with nodes representing commits. The background should be tech-inspired, with a faint, abstract pattern of code or binary digits. The colors should be a mix of blues and greens, associated with technology and coding, to create a modern and clean look. Include the title 'Revert Merge in Git' at the top or bottom of the image in a clear, readable font.

by Avalon Studios

Given that the master branch is protected and direct pushes are not allowed, a common workflow involves creating a new branch, reverting the merge commit there, and then creating a pull request (PR) to merge those changes back into master. Let’s go through this process with a practical example.

Step 1: Create a New Branch

First, ensure your local master branch is up to date:

git checkout master
git pull origin master

Then, create a new branch from master to perform the revert:

git checkout -b revert-merge-branch

Replace revert-merge-branch with a meaningful name for your situation.

Step 2: Find the Merge Commit to Revert

Use the git log command to find the merge commit you want to revert. You’re looking for the commit at the point where your feature branch was merged into master.

git log --oneline --graph --decorate

Identify the merge commit by its message and note its commit hash.

Step 3: Revert the Merge Commit

Now, revert the merge commit on your new branch. Assuming the merge commit is the first parent from the master branch:

git revert -m 1 <commit-hash>

Replace <commit-hash> with the actual hash you found.

Step 4: Resolve Any Conflicts

If the revert process introduces conflicts, Git will notify you. Resolve these by editing the conflicted files. After resolving each conflict, add the file to the staging area:

git add <file-name>

Once all conflicts are resolved, finalize the revert:

git revert --continue

Step 5: Commit and Push the Revert

If you had to resolve conflicts, the git revert --continue command would have already created a commit for you. Otherwise, the git revert command creates the commit automatically. Now, push your branch to the remote repository:

git push origin revert-merge-branch

Step 6: Create a Pull Request

Go to your repository’s page on GitHub, GitLab, Bitbucket, or whichever service you use. You should see an option to “Compare & pull request” for your newly pushed branch. Click on it.

Fill in the details of your pull request:

  • Title: Make it descriptive, e.g., “Revert merge of feature-xyz into master”.
  • Description: Explain why you’re reverting this merge. Provide any additional context that reviewers might need.

Assign reviewers as required by your team’s workflow and create the pull request.

Step 7: Review and Merge the Pull Request

Once the pull request is created, your teammates will review the changes. This process varies by team but may involve automated checks, manual code reviews, and possibly a build verification test (BVT).

After the pull request is approved and any required checks pass, someone with the necessary permissions can merge it into the master branch, effectively reverting the original merge commit.

Note on Protected Branches

Working with protected branches is a common practice to safeguard important branches like master or main from direct modifications. This workflow ensures that every change undergoes review, maintaining the integrity and stability of the branch.

This example covered a straightforward revert scenario. Real-world situations might require additional steps or considerations, such as coordinating with your team or handling complex merge conflicts.


Leave a Reply

Your email address will not be published. Required fields are marked *