Remove commits from a single branch in Git
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 t开发者_运维百科he 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.
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
If you don't mind that feature2 is merged back in on P
instead of M
, here is a relatively simple solution.
- Rebase I-J-K onto D, look at the section title "more interesting rebases
- Rebase O-P onto H. same way as done in step 1
- Finally merge feature2 into feature1 with
git merge feature2
while having feature1 checked out.
If you want K
merged into a particular commits, the rebasing gets a little trickier.
精彩评论