Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Last active November 17, 2017 16:04
Show Gist options
  • Save wookietreiber/290e909a39cf7a02c8e89290e6fc4fed to your computer and use it in GitHub Desktop.
Save wookietreiber/290e909a39cf7a02c8e89290e6fc4fed to your computer and use it in GitHub Desktop.
extract git history from only a few files

install git extract

  1. clone this repository

    git clone https://gist.github.com/wookietreiber/290e909a39cf7a02c8e89290e6fc4fed git-extract
    
  2. make git-extract script available to your shell

    mkdir -p ~/bin
    cp git-extract/git-extract.sh ~/bin/git-extract
    chmod +x ~/bin/git-extract

    point your PATH to ~/bin (default in most distros)

workflow

  1. create a fresh clone of the repository

    git clone repo
    
  2. run git extract on the files you want

    git extract file.a file.b file.c
    

    this rewrites the complete history and keeps only the commits that changes the files you specified

  3. clean up old git references

    git for-each-ref --format="%(refname)" refs/original/ |
    xargs -n 1 git update-ref -d
    
  4. (optional) rewrite commit messages

    git rebase -i --root
    

    replace pick with reword because in interactive rebase you will see only the first line of the commit message. if you don't want to change the message just close the editor without changing the text.

  5. remove remote

    git remote remove origin
    
  6. create new remote (use GitHub, GitLab or whatever)

  7. add new remote

    git remote add origin repo
    
  8. push to new remote

    git push -u origin repo
    
#!/bin/bash
git filter-branch --prune-empty --index-filter "
git read-tree --empty
git reset \$GIT_COMMIT -- $*
" -- --all -- "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment