Why can't I reload the merged theme branch?
Let's say I have this situation in my git repo
A--B--C--D master
|\
| E--F topic
|
\
G--H other
and I want to rebase topic
to a branch other
. This works great:
git rebase --onto other master topic
which gives me
A--B--C--D master
|
\
G--H other
\
E--F topic
But if topic
it was already merged into master ( git merge topic
), then the above command rebase
no longer works. If I try, I seem to get something like this:
A--B--C--D--M master
|\ /
| E--F----
|
\
G--H other / topic
whereas I wanted this:
A--B--C--D--M master
|\ /
| E--F----
|
\
G--H other
\
E'--F' topic
Why is this and how can I reinstall topic
on other
, even if it was already merged with master
?
EDIT: Updated bottom diagram to make it clear that E
u F
are not identical commits - but introduce the same changeset.
EDIT 2: I really want it as if I first create a copy of topic
(say topic-rebased
) which I reinstall before other
, before I then merge topic
into other
:
git checkout topic
git checkout -b topic-rebased
git rebase --onto other master topic-rebased
git checkout master
git merge topic
This works great. But rebase no longer works if the topic has already been merged into master
.
source to share
You can not expect the two of each E
, and F
will exist in your repository. If they have different parents (what they do: one set has A
as parent, the other has H
as parent), then one E
will be different E
than the other.
In your case, if you want this result, you should look into cherry picking :
git checkout topic # puts you on `H`
git cherry-pick E
git cherry-pick F
This applies commits E
and F
your current branch and, in fact, it will create a copy. Thus, you should get the desired result.
source to share
If your story is the same as your example, this will do the following:
git checkout topic
git rebase --onto other --root
I did this to confirm:
% git init .
(add commit a)
% touch a; git add a ; git commit -m "a"
% git checkout -b topic
(add commits e and f, elided for brevity)
% ls
a e f
% git checkout master
% git checkout -b other
(add commits g and h)
% ls
a g h
% git checkout master
(add commits b, c, and d)
% ls
a b c d
% git merge topic
Merge made by the 'recursive' strategy.
e | 0
f | 0
2 files changed, 0 insertions(+), 0 deletions(-)
% ls
a b c d e f
% git checkout topic
% ls
a e f
% git rebase --onto other --root
First, rewinding head to replay your work on top of it...
Applying: e
Applying: f
% ls
a e f g h
It will automatically find the common parent and add (really cherry pick) only commits from the theme branch, even if it's already merged into master.
source to share