Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git workflow issue. Remove a merged branch from another branch

Tags:

git

I have a feature branch that has been merged multiple times with a development branch over the the last few weeks. Before we merge it to master branch we want to remove the feature branches code thats been merged into the development branch over the last few weeks. Is there a unified way of doing this?

The merges were created with --no-ff, so the commits in the new branch should have their own objects.

_______ master ______________________________
                  \                   /
                   \                 /
                    \__hofix-1_____C/
                                    \
_ development _______________________\____________
\                    /          /         /
 \                  /          /         /
  \_feature-test__A/_________B/________D/

So I want to remove the merged commits A, B and D from development, while still retaining hotfix-1, merge C, and still continue to work on the feature branch.

like image 546
madphp Avatar asked Jun 28 '13 14:06

madphp


People also ask

How do I remove a branch from a merged branch?

Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

Should I delete merged branches?

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).

What happens when two branches are merged in git?

Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.


2 Answers

The question is somewhat fuzzy. You have a dev branch and want to unmerge several incoming merges, leaving the rest? If so, you can use rebase -i -p with some low point, deleting the merge commits form the todo list. Note that you shall re-check all the commits you pulled stuff under, as next work might build on it.

Another approach is to use git revert on the top for the unwanted changes. It gets the final result done, but likely leave you with a poor quality history.

like image 112
Balog Pal Avatar answered Oct 04 '22 07:10

Balog Pal


If I understand your situation correctly, I don't think you need anything as heavy as rebase, revert and the like. You can simply do this:

  1. look through the development log to find the commit hash just before merge A, and create a new branch there called "new-dev":

    git checkout -b new-dev somecommithash

  2. now merge in C from the hotfix-1 branch:

    git merge C

... and you're done. new-dev branch now has the contents of development, merged with C, minus A B and D.

If you now want to make the developer branch point to the same place as the new-dev branch, that's as simple as:

git branch -f developer new-dev

But for safety you might want to mark the old developer branch location with a tag or branch before forcibly moving it to the new location.

like image 38
Magnus Avatar answered Oct 04 '22 06:10

Magnus