Home > Back-end >  How do you extract the commits of a single file into a fresh git tree?
How do you extract the commits of a single file into a fresh git tree?

Time:06-14

I'm splitting out a library from an existing source tree so the library can be maintained separately.

How do you extract all the commits for one file and import them into another tree, but without all the .git/objects clutter from the original tree?

I was thinking something like this:

cd /new/tree
git init

cd /original/tree
git show --reverse  815f0f..HEAD -- lib/PDL/Opt/Simplex/Simple.pm | \
    (cd /new/tree ; git am)

but it gives errors and won't import.

I also thought of just branching, deleting everything, and then fetching the pruned branch into a new tree---but (I think) that would import all the other tree's commit data in .git/objects that would be orphaned in the new tree.

Is there a "proper" way to do this cleanly?

CodePudding user response:

This worked. I'm open to other answers, too, if you have a better way:

git format-patch --stdout 815f0f..HEAD -- lib/PDL/Opt/Simplex/Simple.pm | \
    (cd ../perl-PDL-Opt-Simplex-Simple/ ; git am)
  • Related