git

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


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.


Solution

  • Use git fast-export.

    First you export the history of the file to a fast-import stream (.fi file). 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