Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a single file from a git repo into a new repo?

Tags:

git

I have a git repo with several directories, and a single file, MyFile.ext.

/
  LargeDir1/
  LargeDir2/
  LargeDir3/
      .
      .
      .
  MyFile.ext

I'd like to start a new repo with just MyFile.ext in it, and keep all the history pertaining to it, but ignore everything else (all the LargeDirs). How can I do this?

For directories, I've successfully used this answer, but I tried that on a single file, and it doesn't work.

I've also tried this answer, which does delete everything except my file, but it also seems to leave all the history around.

like image 739
Kris Harper Avatar asked Sep 13 '16 21:09

Kris Harper


People also ask

Can I pull a single file from a git repository?

GitHub lets you download one file from a repository. This is a useful feature because it means you do not have to clone or retrieve an entire repository to download a particular file.


1 Answers

Use git fast-export.

First you export the history of the file to a fast-import stream. Make sure you do this on the master branch.

cd oldrepo
git fast-export HEAD -- MyFile.ext >../myfile.fi

Then you create a new repo and import.

cd ..
mkdir newrepo
cd newrepo
git init
git fast-import <../myfile.fi
git checkout
like image 171
Roland Smith Avatar answered Sep 18 '22 20:09

Roland Smith