Git extract folder from project with history and tags

This is a short note regarding the use of `git filter-branch` when you want to split a git repository and preserve the history.

I had some issues in finding the correct parameters. In case this also can help others, you can find the command here.

In case you are not yet familiar with the possibilities of `git filter-branch`, I suggest you look into the  Git book: Rewriting history, which has a good introduction to the command.

Now, to start you should clone the repository you want to change (since you will completely rewrite history in the new repos).

 

In short, without further explanation:

ORIG=path_to_original_repos
DEST=destination_of_rewritten_repos
DIR=directory_you_want_to_extract_from_ORIG

git clone $ORIG $DEST && \
cd $DEST && \
git filter-branch --prune-empty --tag-name-filter cat --subdirectory-filter $DIR -- --all

A few notes:

  • --prune-empty will remove all commits that has no relation to the folder you extract
  • --tag-name-filter cat will also rewrite any tags you put on the commits that are rewritten
  • --subdirectory-filter takes a foldername as parameter, but I needed to add -- --all to make it work

 

One Response to “Git extract folder from project with history and tags”

  1. Geert Verschaeve Says:

    I’ve been having problems with Git every I started applying it, especially because a mistake is quickly made. That said, I love how easy it is to undo mistakes. So thanks for the “— –all comment”, I’ve just tested it and to be honest, didn’t even know I could split AND preserve the history.