How to revert a Git Pull Request?

Pull requests are made on git to work with a team collaboratively. In most open source projects, the contributors request for the review and acceptance of a branch, and then depending on the vitality of the code, it is either accepted into the code or not. 

However, what happens when it is not merged and needs to be reverted? This is why we’ll tell you how to revert a git pull request. 

Why is a Git pull request needed?

You want to avoid being in a position of reverting the pull request as often as possible, but sometimes, you just have to do it. And a lot of situations are beyond your control. So here are some of the reasons: 

  • The pull request was accepted and merged by mistake. This ends up with the original code having a bunch of broken code. 
  • The commits made to a branch consist of unintended changes 
  • The commit adds issues to the code affecting the functionality of the codebase
  • A conflict occurs with a different change in the codebase, so you need to revert until a solution is found 
  • A new feature change was intended to be experimental but caused issues to the stability of the codebase 
  • The pull request was associated with a different branch, which can happen when there is a complex branch strategy 

How to revert a Git pull request?

There are multiple ways you can revert a git pull request; some are safer than others. Here are some of the methods: 

Creating a pull request for the revert 

Whenever a revert is made, it creates one revert of the merge commit. 

If you have write permissions, go ahead with the following steps: 

  • Click on pull requests in your repository.
  • Select the pull request you want to revert
  • Click on the revert option that you see at the right bottom
  • If you don’t have write permission, request your administrator for it
Reverse using the git revert command

The Git revert command helps you create an inverse to the introduced changes and adds a new commit with these changes. 

Here are the steps: 

  • Open your terminal and navigate to the repository where you want to revert the pull request 
  • Use git log to show the commit history and find the ID of the commit you want to revert 
  • Input the commit ID and execute the git revert command.
  • Provide a detailed Git commit message to clarify the reason for performing the Git revert

This step doesn’t remove the changes made but adds changes to negate the pull request. 

Reverse using git reset

The previous method altered the new reality caused by the merged unneeded pull request, but Git reset is like going back in time to change what was done. 

While this might have unintended consequences, it might still be a route you can take if needed, and here is how you can do it. 

  • Find the commit hash that you want to revert to using the git log.
  • Use git reset with the –hard flag and the commit hash to reset the branch to that commit—for example, git reset –hard abc123.
  • Force-push the changes to the remote branch using git push -f.

Undo your pull requests but be smart about it

Pull requests are an integral part of working collaboratively, and merging them by mistake or without enough review can cause many issues to the codebase. Then reverting this process can have other consequences you want to avoid. 

Therefore, have an internal process to merge pull requests. Ensure everybody is on board with the code reviews needed for the process and a checklist to denote when a merge is acceptable. 

We at Typo can help you improve your engineering processes and deploy quality code faster. You can quickly get started with our tool on Git for a seamless experience. 

Want to understand all the ways we can accelerate your engineering team? Book a demo with the Typo team today