Search code examples
gitextractgit-filter-branch

How to extract one file with commit history from a Git repo with index-filter & co?


I have a Git repo converted from SVN to Mercurial to Git, and I wanted to extract just one source file. I also had weird characters like (an encoding mismatch corrupted Unicode ä) and spaces in the filenames.

How can I extract one file from a repository and place it at the root of the new repo?


Solution

  • There is a new command git filter-repo nowadays. It has more possibilities and better performance.

    See man page for details and project page for installation.

    Remove everything except src/README.md and move it to the root:

    git filter-repo --path src/README.md
    git filter-repo --subdirectory-filter src/
    

    --path selects the single file and --subdirectory-filter moves the contents of that directory to root.