TKR TKR - 2 months ago 15
Git Question

Custom git clean filter modifies file in commit but not working directory

I have a custom .gitattributes entry like so:

*.csproj text filter=csprojsort

The corresponding filter is set up like so in .git/config:

[filter "csprojsort"]
clean = "path/to/repo/csprojsort.exe"

Upon commiting a .csproj file with changes, the filter executes as intended by sorting the file alphabetically and then proceeding to commit the changes. If I do a
git show path/to/file.csproj
, the changes show up.

However, the file does NOT change in my working directory, and git does not recognize that the current state is out of sync. Only when I checkout another branch and then checkout the original branch does the file update on disk.

Any clue what is behind this? Thanks.


That sounds like the filter is working as intended. The documentation says a clean filter is "...used to convert the content of a worktree file to a blob upon checkin", which is exactly the behavior you're seeing. It doesn't modify files in your working directory.

A smudge filter does the opposite: when you check out a file, the smudge filter runs before the data is written to a file.