Turns out to be quite simple, the answer found here. Suppose you’re on a branch branch
. Perform these steps:
-
create a temporary branch from the commit after you want to insert the new commit (in this case commit
A
):git checkout -b temp A
-
perform the changes and commit them, creating a the commit, let’s call it
N
:git commit -a -m "Message"
(or
git add
followed bygit commit
) -
rebase the commits you want to have after the new commit (in this case commits
B
andC
) onto the new commit:git rebase temp branch
(possibly you need to use -p
to preserve merges, if there were any - thanks to a no longer existing comment by ciekawy)
-
delete the temporary branch:
git branch -d temp
After this, the history looks as follows:
A -- N -- B -- C
It is of course possible that some conflicts will appear while rebasing.
In case your branch is not local-only this will introduce rewriting history, so might cause serious problems.