简体   繁体   中英

How to revert back the merged branch from master?

I'm using BitBucket.Already I pushed a branch to master and merged with master.Now I need to revert the pushed branch from master.How to do it?

In my case: I have to revert last five commits and I need go back previous code.How to delete or revert the commit?

Note: Currently I don't have any branch because already merged with master and deleted the branch.I have only one master branch.

Is possible to revert the merged branch again?

It would help greatly to know what your history looks like. I'm going to use Dungeon Crawl's history as an example, and show you how to undo a merge commit:

Let's say it currently looks like this:

$ git log --graph --online --decorate -n 5
*   94e1b85 (HEAD, master) Merge branch 'next'
|\  
| * 9312efc Don't generate Lehudib's crystal spear the unrand.
| * 518382a Make Sticks to Snakes only work on missiles
| * 6e8ccac Let monsters pick up Elf:$ loot
* | 9ca6e02 Distinct manual tiles
* | a16bb39 Restore trunk changelog entries removed by 673ecc60.
* | 1d9921b Touch the 0.13 changelog header with the tentative release date.

You'll find git log --oneline --graph --decorate useful here. --graph will help you see branches & merges, --oneline just keeps it short, and --decorate will annotate branch and tag names onto the commits. (This is such a useful command that I have an alias for a slightly more customized version of this.)

See that 94e1b85 commit? It's a merge commit. Let's say we wanted to undo it. Let's back up stuff, just in case:

# Save master, just in case:
$ git checkout master
$ git branch old-master

First, let's restore the old branch. I'm going to call it other-branch . Either:

$ git branch other-branch master^2

Which means "create a new branch named other-branch , at the commit which is the second ( 2 ) parent ( ^ ) of master . You can also just name it by the hash, which you can see in the git log command I've used above.

$ git branch other-branch 9312efc

History should now look like:

$ git log --graph --online --decorate -n 5
*   94e1b85 (HEAD, master) Merge branch 'next'
|\  
| * 9312efc (other-branch) Don't generate Lehudib's crystal spear the unrand.
| * 518382a Make Sticks to Snakes only work on missiles
| * 6e8ccac Let monsters pick up Elf:$ loot
* | 9ca6e02 Distinct manual tiles
* | a16bb39 Restore trunk changelog entries removed by 673ecc60.
* | 1d9921b Touch the 0.13 changelog header with the tentative release date.

Now, change where master points:

$ git checkout master $$ git reset --hard HEAD^

(you can replace HEAD^ with a commit hash here too. There's an implicit 1 after the ^ .)

Check your work with:

$ git log --oneline --graph -n 120 --decorate old-master
*   94e1b85 (old-master) Merge branch 'next'
|\  
| * 9312efc (other-branch) Don't generate Lehudib's crystal spear the unrand.
| * 518382a Make Sticks to Snakes only work on missiles
| * 6e8ccac Let monsters pick up Elf:$ loot
* | 9ca6e02 (HEAD, master) Distinct manual tiles
* | a16bb39 Restore trunk changelog entries removed by 673ecc60.
* | 1d9921b Touch the 0.13 changelog header with the tentative release date.

Once you get things to a state you want, to push it up to BitBucket you will mostly likely need to git push --force origin master . A word of caution: This will forcefully set master on Bitbucket to whatever you've set it to using the above. Also: We're effectively re-writing history. If anyone else has based changes on that merge commit that we've undo, we're going to cause problems for them. (They'll need to rebase their commits onto our newly re-written history.)

Ok, so you will need to do a force push to fix this up. I wouldn't do a revert of the merge, that will give you problems. Note, the force push will mean that you are rewriting history, so you will need to let your other developers know that you are doing this.

1./ reset master back to the master side parent commit
2./ recover your private branch, so you don't lose your commits.
3./ force push master.

this diagram hopefully makes it clear. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM