DEV Community

Cover image for How do I revert a merged pull request?
Arun Krish
Arun Krish

Posted on

How do I revert a merged pull request?

Here I am discussing how to revert a merged pull request and again push the branch. I am sharing this because I faced this issue while developing something...

Keep in mind that the working branch is "master" and the feature branch is "navbar".

If you don't know how to push your code into github, please visit my previous post here

Here we go...

The problem 1: I created a pull request in github and merged. But that isn't the actual code, or it's not working in the master branch after merging.

The merged file was navbar.

The solution: Let's checkout branch into master and pull the code from github, then check git log.

git checkout master
git pull origin master
git log
Enter fullscreen mode Exit fullscreen mode

Git terminal

You can see a commit message like "merge pull request."

You can see the merge commit message and commit id
You can revert the commit using that commitId and a git command like

git revert -m 1 714013e1e7dc98787b73b9da197994b770d0c1d7

Enter fullscreen mode Exit fullscreen mode

You can write the commit message for reason for reverting in the opening window and press ctrl x. Then push the code into github.

git push origin master
Enter fullscreen mode Exit fullscreen mode

Now you reverted your pull request successfully. You can see that your merged files are removed now and a new commit message. Eg: here navbar file removed.

Navbar removed, you can see the commit message in the repo

The problem 2: What What happens if you want to work in the navbar branch and create a new pull request again?

The solution: checkout into the branch navbar and work in the navbar branch and create a pull request.

git checkout navbar
Enter fullscreen mode Exit fullscreen mode

After editing the file, add, commit and push.

git terminal

git add .
git commit -m "navbar edited"
git push origin navbar
Enter fullscreen mode Exit fullscreen mode

Go to GitHub and select the navbar branch.

Git hub selecting navbar branch

Creating pull request by clicking contribute button
You can see a contribute button; by pressing that, you can create a new pull request.

If there aren't any conflicts, it can merge. If there are any conflicts, first of all, go to your IDE checkout to the master branch and pull the code from github. Then checkout to branch navbar, then merge master into navbar.

git chekcout master
git pull origin master
git checkout navbar
git merge master
Enter fullscreen mode Exit fullscreen mode

Now you can see the conflicts and solve the conflicts in the merge editor (hoping you are using vs code), select the incoming code.

VS Code Merge Editor, select incoming code

Complete the merge, add, commit, and create a pull request once again.

Git terminal
Now you can create a pull request for the navbar branch by selecting the navbar branch, then clicking the contribute button and opening the pull request, create pull request.

GitHub conflict solved
You can see that conflicts are solved.

Thankyou
Happy coding:)

Top comments (0)