70

I've created a pull request and I've merged it to an wrong branch. How can I revert it?

As far I've been to figuring out taking a look over there, I'm able to hard reset the destination branch... but, how about the pull request in origin repository?

I'm using Bitbucket and I've created the pull request from SourceTree (opening the Bitbucket page).

I've three branches I'm working on: master, dev and create-alias. create-alias was set up from dev and dev from master: master -> dev -> create-alias. The problem is I made a pull request from create-alias and I did merge it into master instead of dev.

I'm working on create-alias branch right now. The last commit on create-alias is 6ee20f9 and the merged commit on master is be36f72.

Could you write me down a bit about who to step-by-step revert it?

As far I've been able to figure out:

  1. checkout on master.
  2. revert -m 1 6ee20f9.
  3. push.
  4. checkout on create-alias and going on working.

Isn't it?

shim
  • 9,289
  • 12
  • 69
  • 108
Jordi
  • 20,868
  • 39
  • 149
  • 333

4 Answers4

112

Unfortunately, there is no "Revert Pull Request" feature on Bitbucket as of this writing, but a feature request exists for it.

Note: Before you proceed, make sure your working copy is clean, with no uncommitted or unpushed changes.

So, you'll have to revert the merge in Git. First, find the SHA hash of the merge commit.

On the command line, this is:

git checkout <branch>
git pull
git log

Then, we revert the merge commit and push it:

git revert -m 1 <SHA-1>
git push

In SourceTree, first checkout the branch in question, then Pull. Find the merge commit in the log window, then right click it, and click Copy SHA-1 to Clipboard.... Then go to Actions --> Open in Terminal. Once the terminal opens, type:

git revert -m 1 <SHA-1 (from clipboard)>
git push

Unfortunately, SourceTree doesn't have a way to simply right-click and revert a merge, but a feature request exists for it.

shim
  • 9,289
  • 12
  • 69
  • 108
Will
  • 24,082
  • 14
  • 97
  • 108
41

Bitbucket has a "revert" button on Pull Requests now.

enter image description here

Note it doesn't automatically update the branch the original PR was merged into it. It creates a new branch with a commit that reverts the PR:

enter image description here

You then can create a PR from this branch and merge it.

oknate
  • 1,068
  • 10
  • 14
  • 1
    I don't see this in our Bitbucket. We're running Atlassian Bitbucket v5.15.0. Enterprise edition, or something like that. – David Hempy May 22 '19 at 22:17
  • 2
    @DavidHempy It might only be available on the cloud version, I can see it there. Note if you're using the "new" PR view you'll have to switch back to the old version to see this button – Josh G Jun 05 '19 at 19:26
  • 2
    I would really want this button right now, but our local bitbucket doesn't have it yet :( – Stefan Jun 18 '19 at 14:35
  • 3
    This is nice but what to do if you merge feature-branch into master, revert master then merge same feature-branch into develop and want to merge into master later. Because those commits are in the history they will just be super-ceded by the reversion when trying to merge. Is there an alternate strategy? – Josh Morel Nov 19 '19 at 00:12
10

Bitbucket now has the 'Revert Merged Pull Request' feature released.

Yash Dixit
  • 216
  • 3
  • 12
Pieter Du Toit
  • 429
  • 5
  • 22
4

With bitbucket, we can't revert a merged pull request. The revert option which is present in the pull request will create a new branch with a new head(before this commit). That branch later you have to merge into your master/release branch. This is all as communicated by the official Bitbucket doc. enter image description here

Rashid
  • 343
  • 4
  • 14