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
revert-merge-branch with a meaningful name for your situation.
Step 2: Find the Merge Commit to Revert
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
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
git revert -m 1 <commit-hash>
<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
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.