Psycho
Psycho

Reputation: 1883

More clever git filter-branch --subdirectory-filter

I have a sizable project with a long history behind it and I'm trying to put certain files into their own repository while keeping their whole history of changes.
Prior to do that, I moved all the files that I need to keep in their own project/subdirectory, so I could just clone my repository, use git filter-branch --subdirectory-filter but that throws away the whole history of the files since the subdirectory is very new.

What I'm looking for is a way to say "keep all commits which lead to these files", so I would take all the files in that subdirectory and walk the commits backward saying "was that file in this commit? Yes keep the commit and the file in that commit".

Is there a way to do that relatively painlessly?

Upvotes: 2

Views: 1571

Answers (1)

VonC
VonC

Reputation: 1323125

Maybe you need to modify your original repo first, in order for the history of those files to look like they always were in that subdirectory.
Then you can apply the filter-beanch.

See "How can I rewrite history so that all files are in a subdirectory?":

rewrite the commits so that anywhere project/subdirectory doesn't exist, it is created and all files are moved to it:

git filter-branch --prune-empty --tree-filter '
if [[ ! -e project/subdirectory ]]; then
    mkdir -p project/subdirectory
    git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files project/subdirectory
fi'

Now the history will be recorded as if all files were always located in project/subdirectory.

Upvotes: 1

Related Questions