Git create branch from range of previous commits?

You can do this with cherry-pick. If your history of long_branch looks like this:

A-B  <-- master
   \
    C-D-E-F-G-H  <-- long_branch

and you want to move the contents of, say, F through H to a different branch, say, short_branch, which is based off of master:

git checkout master -b short_branch

which gives

A-B  <-- master, short_branch
   \
    C-D-E-F-G-H  <-- long_branch

then… (note that the commit range is E..H; the left side is non-inclusive for the range)

git cherry-pick E..H

which gives:

    F'-G'-H'  <-- short_branch
   /
A-B  <-- master
   \
    C-D-E-F-G-H  <-- long_branch

Note that I’m referring to the new commits as F', G', H' – this is because while they will contain the same effective changes as F, G, and H, they won’t be the actual same commits (due to different parents + commit times).

Leave a Comment