Your question is ambiguous, fortunately the drawing sheds some light about what you want.
If I understand it correctly, this is the input status of the repo
M--N--P <-- Branch B
/
O--O--X--Y--Z <-- Branch A
and this is what you want to achieve. I used letters to denote the commits.
M--Y--Z--N--P <-- Branch B
/
O--O--X
First you have to create a new branch (C
) that points to M
to keep it visible until the end of the operation
git branch C B~2
Replace B~2
with the actual number of commits you have from N
to B
(including both) or use absolute references (existing branches or commit hashes).
The following command moves N
-P
on top of Z
. It moves all the commits that are in B
but are not in C
on top of A
# Move N-P on top of Z (A points to Z, B points to P, C points to M)
git rebase --onto A C B
The last step is to move all commits that are in B
but are not in C
on top of C
:
# Move Y-Z-N-P on top of M (B still points to P, A still points to Z)
git rebase C B
The repo now look like this:
+-- Branch C
v
M--Y'-Z'-N'-P' <-- Branch B
/
O--O--X--Y--Z <-- Branch A
Branch A
still points to the original Z
commit; also the original Y
commit is still in place. The current branch is B
.
Commits Y'
, Z'
,N'
and P'
are copies of Y
, Z
, N
and P
. They differ from the originals only on the commit date and parent; the are no code changes.
The branches A
and C
can be removed now. (git branch -D A C
).
Read more about git rebase
.