How to undo a merge on Bitbucket?
Asked Answered
U

6

71

I've created a merge (into the 'master' branch) that's now on a Bitbucket repo. Long story short: I need to undo that merge.

I know that you can do this at the Github site itself, but Bitbucket doesn't have that feature. I'm not clear on how to do this with Git without causing a mess.

Urmia answered 4/5, 2016 at 19:27 Comment(0)
D
122

You need to first clone the repository on your local system (you can get the repo URL in SSH or HTTPS format from the "Overview" page of the repository in Bitbucket):

git clone [email protected]:my/repo.git
-or-
git clone https://[email protected]/my/repo.git

git checkout master

.. then revert the most recent commit. First list the available commits with:

git log

.. then select the commit before the merge:

git reset --hard 72ead1c4c1778c23c277c4f15bbb68f3bb205f54

.. where the hash is the hash of the commit before the merge (from the log). Finally, force-push the changes back to Bitbucket, overwriting history.

git push -f

Naturally if the repo is shared, and its other users have pulled your most recent commit and built atop it, they won't be happy. So in that case be sure to notify everybody of what you're doing.

revert, as mentioned in the other answers is another option; it keeps the commit you made, but modifies the repository further (with a new commit) in such way that it undoes the changes you made. Whether you want to use revert depends on whether you want the information in your commit to remain in the repo history or not.

For more detail on undoing changes in git, see a good tutorial page by Atlassian.

Datcha answered 4/5, 2016 at 19:39 Comment(1)
An important note about using git revert to undo a merge (using the -m option): git revert --help says: Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want. -> So if reverting the merge needs to be undone, the solution is not to merge the previously merged (and then reverted) branch again but to revert the revert commit.Headley
M
25

A "Revert pull request" feature was implemented in Bitbucket in 2017.

To revert a pull request:

  1. From the pull request, click the Revert button in the top right. (Optional) From the Revert pull request dialog, change the Branch name for the new branch you're about to create.
  2. Click the Revert button. Once you click Revert, Bitbucket creates the new branch. Even if you cancel the pull request, the revert branch remains in the repository.
  3. The Create a pull request page opens with the revert branch as the source. After you add your reviewers and make additional changes, click Create.

Source: the docs.

BitBucket Revert pull request

Mara answered 8/12, 2017 at 19:5 Comment(3)
I can't find the revert option. I guess this only available in bitbucket cloud, not in bitbucket serverMeticulous
This answer has nothing to do with the question. He's talking about a merge, not a PRCharnel
Usually a merge is through a PR, no? This flow will get the source code back into what it was before the PR that did the merge.Trochelminth
H
6

I would suggest doing a revert instead, since you are reverting a public repo.

git revert HEAD
git push -f origin
Horseback answered 4/5, 2016 at 19:44 Comment(1)
I like the concept except that I wouldn't do the -f on on the push. Just as you mentioned, branch is public, so if someone just committed something else the forced push will discard them. Regular push should do the job, and, if other changes are already in the remote, the push will be rejected and give you the chance to incorporate new upcoming changes.Delly
A
5

to undo the changes of a commit: git revert <commit id>

Acierate answered 4/5, 2016 at 19:44 Comment(0)
C
4

First, on your local machine, on the branch you merged into (e.g. master):

git revert -m 1 HEAD

or:

git revert -m 1 <merge commit hash>

Then push to origin.

Alternatively, if you cannot push directly to master, create a new branch off of master first before reverting, then push and create a new PR with this new branch against master and merge on bitbucket.

Calyptra answered 6/5, 2021 at 15:51 Comment(1)
Here you can find clear details about the option: "-m 1": git-tower.com/learn/git/faq/undo-git-mergeGaspar
C
0

Click on Commits, then find the pull request #number:

see screenshot

Open the pull request page, then click on ..., then click on "Revert*:

see screenshot

Crap answered 20/6 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.