I recently had to extract a subdirectory of an existing git repository towards its own new repository. And I wanted to keep all the commit information and history that was related to this directory.
EDIT: Even if the solution I used was simpler than I would have expected, I was far from knowing that GIT had a 'filter-branch' command which does the job perfectly well (and it is documented in ProGIT). Thanks Jon for the tip.
sketching the tools used
First step, is to retrieve only the commit Hashes related to this directory:
git log --format=%H -- MY_DIRECTORY
to filter out only the commit Hashes related to this directory. Then:
git format-patch HASH -1
will be used to output patch files with the commit information (subject, author...)
Then, with some sed you'll have to change the directories used in all the outputed patch:
sed -i s%MYOLDDIRECTORY%MYNEWDIRECTORY%g *.patch
The new directory in my case was "" (nothing) as it was in the root folder.
Finally, you'll have to apply your patches to your other repository with:
git am *.patch
Complete commands used
The complete commands that I've used:
cd PARENT_OF_MYDIRECTORY git log --format=%H -- MYDIRECTORY | tac | \ (i=0 while read h; do i=$[$i+1] git format-patch $h -1 --start-number $i done)
This produces NNNN-*.patch files, one for each commit:
sed -i s%MYDIRECTORY%%g *.patch
This will update the paths in all patches:
mv *.patch MY_NEW_REPO cd MY_NEW_REPO git am *.patch
A quick check with gitk ensures me that message subject and commit were transfered ok.
Et voilĂ !