This page looks best with JavaScript enabled

Extract a File from Git Repo to its own, Preserving the History

 ·   ·  ☕ 1 min read

Today I will walk through how you can take out a single file from a git repository and create its own repository with all the file commit history preserved.

When I started working at my workplace I initialized Maya script directory to git. At that time one repo was looking enough for entire folder. But when I started working with a tool, I realized an entire repo would be good for that tool. But it was too late to create another repo because I had already made 12-15 commits on that tool and I don’t wanted to lose the changes I made.

I researched it on internet and found this.

1
2
3
4
5
cd repository
git log --pretty=email --patch-with-stat --reverse --full-index --binary -- path/ </code>to/file_or_folder > patch
cd ../another_repository
git init
git am < ../repository/patch

This is an excerpt from a StackOverflow question.

Basically what it is doing it is, creating patch based on the file history, along with the commiter and other data. And after creating another repository applying the patch. There is even no need to copy the files. The code is pretty self explanatary.


If you liked this post, please subscribe to my newsletter below.

Share on

Santosh Kumar
WRITTEN BY
Santosh Kumar
Santosh is a Software Developer currently working with NuNet as a Full Stack Developer.