Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a single merged branch from master branch

Tags:

git

branch

In my master branch, there're following local branches that have been merged, but I would like to remove the local_branch3 from master:

local_branch1
local_branch2
local_branch3
local_branch4

after removing the local_branch3 from master, I would like it to remain a local branch (only deleted from master).

I've check Steve Harman's blog on this similar issue, but that seems also deleting the local branch for ever.

Edit: to clarify what I meant, as I posted in one of the below comments:

I would like the master branch to undo the changes resulting from local_branch3 merging, while leaving local_branch3 untouched.

The reason being that I would like to keep the master branch deliverable while I tweak on local_branch3.

like image 959
TonyGW Avatar asked Dec 04 '14 14:12

TonyGW


People also ask

Can we delete a merged branch?

There's no problem in deleting branches that have been merged in. All the commits are still available in the history, and even in the GitHub interface, they will still show up (see, e.g., this PR which refers to a fork that I've deleted after the PR got accepted).

How do I revert a merged branch from master?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.


2 Answers

Maybe you mean reverting local_branch3 from master?

If so, first check SHA1 value of the commit which merges local_branch3. And

git checkout master
git revert -m 1 <<SHA1 value>>

Then no local_branch3 codes in master branch. And locah_branch3 will remain.

like image 123
shirakia Avatar answered Sep 18 '22 08:09

shirakia


If I'm reading the question right, shirakia's answer (revert the merge commit's changes) is the right one, it works as advertised and OP should use it.

But it's worth pointing out what happens if further work on local3 makes the whole of it valid, and you naturally want to reapply the original work along with those later corrections.

The local3 commits as of that merge are now part of the merge commit's history. Because the base content for any further merges from local3 is at the new merge base, all of that history is effectively marked as (really, it has been) already correctly applied: any later merge sees only changes made since then. The revert didn't undo that part (in fact, that was the whole point of the revert, to not remove that commit).

Here's a well-known discussion of how to reapply the changes if further development on local3 makes the whole thing mergeable again (you revert the revert, you put the changes back yourself).

Another perhaps simpler way of putting it is, the right way to undo any commit you don't want to remove from your history is to revert it. That includes any later decision to undo the reversion.

like image 41
jthill Avatar answered Sep 18 '22 08:09

jthill