1

I am experimenting with git filter-branch, and I found out that this command:

git filter-branch --prune-empty --subdirectory-filter directory -- --all

apparently works equally well without the --prune-empty switch. The manpage says this:

--subdirectory-filter <directory> Only look at the history which touches the given subdirectory. The result will contain that directory (and only that) as its project root.

I find it a little ambiguous. Does that mean that if a commit does not touch the directory, it will be omitted from the result (even if --prune-empty was not given)?

I am not that well-versed in shell scripting to be able to figure it out quickly from the source code (git-filter-branch is written in sh), so I would appreciate any comments from more experienced people, preferably with the relevant part of the source cited.

In the case anyone would like to perform an experiment similar to my one, here are the commands I use (I use bash, though I am pretty sure this is shell-agnostic).

cd /tmp
rm -rf repo
git init repo
cd repo
mkdir directory
echo "some file in the root dir" > some-file.txt
echo "another file in the directory dir" > directory/another-file.txt
git add .
git commit -m "Initial commit"
echo "added line" >> directory/another-file.txt
git commit -am "Add a line"
echo "An an unrelated commit" >> some-file.txt
git commit -am "An an unrelated commit"
echo "A commit spanning everything" >> some-file.txt
echo "A commit spanning everything" >> directory/another-file.txt
git commit -am "Make huge changes"
git filter-branch --prune-empty --subdirectory-filter directory -- --all

1 Answer 1

0

Does that mean that if a commit does not touch the directory, it will be omitted from the result (even if --prune-empty was not given)?

Yes. The --subdirectory-filter removes all commits not touching directory (or its children) from your history.

Let's say you start with 20 commits where 5 make changes to directory. After you run git filter-branch --subdirectory-filter directory you will be left with 5 commits. The other 15 commits have been pruned. This means that --subdirectory-filter implies --prune-empty.

2
  • 2
    Technically, you can add additional filters beyond the subdirectory filter. These could adjust the file contents of the to-be-copied commit so that they match those of a previously-copied commit. However, if you don't do that, the logic should hold.
    – torek
    Aug 13, 2019 at 6:47
  • Thanks @AlexanderGroß, but how do you know that? I know that by experiment, but how can I be sure that it is always the case and not only in my case because of something I didn't even consider? It would be best to confirm it by pinpointing the exact place in the source code which makes Git behave that way.
    – mbork
    Aug 13, 2019 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.