Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove commits from a single branch in Git

Tags:

git

branch

I have two branches, master and feature1. I was working on feature1 when I realized that I needed to work on something else unrelated. Unfortunately, I forgot to branch from the master, and instead created my feature2 branch from feature1. Now I want to merge feature2 into master, but cannot because there are parts of feature1 in that branch.

How do I remove the feature1 commits from the feature2 branch? Does it involve rebasing?

I feel that if I could change the starting reference point of feature2 to be where master is, that might help, but have no idea how.

EDIT:

Thank you for the answers! I tried rebasing according to @Mark's solution, but realized that the history is more complicated than I originally thought. feature 2 has been merged into other feature branches, and master was merged into feature2 at one point. There are also additional commits on master that are not related to feature2 Everything is still local.

Really, my history is more like this:

A - B - C - D - - - - - - - - - - - - - L - M  master               |                                       |               - I - J - K  feature2                               \              /           \              - E - F - G - H - - - - - -N - O - P  feature1 

And what I want is this:

A - B - C - D - - - - - - - - - -  L - M  master             |\                                       |  - - - - - I - J - K  feature2                               \                     \               - E - F - G - H - - - N - O - P  feature1 

I have also tried:

git rebase --onto 1524b824cfce5856a49e feature1 feature2 // 1524b824cfce5856a49e == D 

But this just sets the branch name feature 2 pointing at 1524, and leaves commits I, J, K with their original parents.

like image 425
Andrew Avatar asked Aug 31 '11 15:08

Andrew


1 Answers

If it's only you working on your feature2 branch and you haven't shared it with other people, it's fine to rebase that branch before merging. You could do the following, for example:

git checkout feature2 git rebase --onto master feature1 feature2 

... which will rewrite your feature2 branch, leaving it so that it consists of all the commits in feature2 since it was branched from feature1, but reapplied onto master. I suggest you use gitk --all or a similar git history viewer afterwards to check that the result of this operation is what you want.

Incidentally, this is exactly the scenario used to explain --onto in the git rebase documentation - see the paragraph beginning:

Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto. [...]


Updated in response to more material in the question:

Starting with your history, which looks like this:

A - B - C - D - - - - - - - - - - - - - L - M  master               |                                       |               - I - J - K  feature2                               \              /           \              - E - F - G - H - - - - - -N - O - P  feature1 

You can get what you want by doing the following:

git checkout feature2 git rebase --onto D H feature2 

This will leave you with history that looks like:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master               |\             | \ - I' J' K' feature2       I - J - K                                 \                           /           \              - - - - - - - E - F - G - H - - - - - -N - O - P  feature1 

Before creating the merge that you want in feature1, you should note down the SHA1sum of O and P. (I'm assuming N is just a normal merge, and not an "evil merge".)

Then do a hard reset to move feature1 back to H:

git checkout feature1 git rest --hard H 

Then you should have the history:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master               |\             | \ - I' J' K' feature2             \              - - - - - - - E - F - G - H feature1 

Now you apparently want to merge K' into feature1:

git merge K'  A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master               |\             | \ - I' - J' - - - - - - - K' feature2             \                             \              - - - - - - - E - F - G - H - Z feature1 

And finally you can cherry-pick the old O and P onto feature1 with:

git cherry-pick O git cherry-pick P 

... to leave:

A - B - C - D - - - - - - - - - - - - - - - - - - - L - M  master               |\             | \ - I' - J' - - - - - - - K' feature2             \                            \              - - - - - - - E - F - G - H - Z - - O' - P' feature1 
like image 72
Mark Longair Avatar answered Oct 02 '22 09:10

Mark Longair