1

I'm looking for a way to rewrite the commits of my git repo so that only the commits related to certain files get preserved. The commits (and in fact the files) that do not belong to my criteria, I don't care if I lose them.

The files I want to preserve are now living on certain subdirectory of my repo, but at the beginning they were living at the root directory of it, then I moved them. I wish to preserve all the commit history related to this files, when they were living at the root, and when they got moved to their own subdirectory, and all their changes.

Suppose the commits related to this files do not touch anything but these files (if I find any commit not covering this requirement, I may use git rebase and split to achieve it).

Is there a way to achieve this, perhaps using filter-branch or something like that?

EDIT: all files I wish to preserve have some prefix in their names: 'foobar' for example. Every other file/subdirectory I wish to remove does not have this prefix.

2 Answers 2

2

I think you can get heavily inspired by Remove sensitive data article by GitHub, in which they walk through the whole repository and rewrite commits for deleting some specific file, in order to remove it from the history.

In your case, you probably can do that for every one of your unwanted commits, and that would be it.

Sign up to request clarification or add additional context in comments.

Comments

2

You definitely want filter-branch. You'll want a tree filter (or index filter) that removes every file except the ones you want to keep. Something like 'find . ! ( -name 'name_to_preserve' -o -name 'another_to_preserve' ) -delete' might work, but as you haven't made it clear how to identify the files you want to keep, I can't provide much guidance there.

2 Comments

files I want to preserve start with a fixed string, like say 'foobar...' . Every other file/subdirectory I want to remove does not start this way
git filter-branch --tree-filter 'find . ! -name "foobar*" -delete' should get you most of the way there, then.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.