I have a linear history like so
----a--b--c--d--e--f---- main
I want to move a series of commits into a new branch and create a merge commit into the old branch, creating a non-linear history like so
----a-------x--d'--e'--f'-- main
\ /
b--c
where x
is the new merge commit.
How do I do this?
Note, the moved commits aren't the most recent ones (see here).
Let's start by creating a few pointers to make things easier.
git branch base <a>
git branch feat <c>
The diagram looks like this.
----a--b--c--d--e--f---- main
^ ^
base feat
Now we merge feat
into base
, creating the merge commit.
git switch base
git merge --no-ff feat
The diagram now looks like this.
----a------x base
\ /
b--c--d--e--f-- main
Now we rebase main
onto base
, moving it onto the new merge commit.
git switch main
git rebase --onto base feat
Finally, we can clean up the pointers, which we don't need anymore.
git branch -d base
git branch -d feat
And we end up with the diagram that looks like this.
----a------x--d'--e'--f'-- main
\ /
b--c