Use git filter to prevent a change from being committed
Especially when working on a paper, a regular problem I end up with is
the following:
Maybe I use a different version of texlive than a co-author, e.g., a more
recent one, and the document won’t compile unless I start making changes in the
preamble.
So I need to make those changes to compile and view the paper but I don’t want
to commit them along with my contributions to the repository — because if I
would do that, the other authors could probably no longer compile the document.
Two inconvenient solutions for this are:
- Use interactive staging for each commit and make sure that this particular
change doesn’t end up in the commit.
Until I accidentally stage it at some point in time and discover that it made it into the upstream branch. Which, at least for me, is really just a matter of time. - Commit the fix to a dedicated branch, make the contributions on top of it and when ready, cherry-pick them into the target branch. Variations of this are working with an integration branch. This solution is a lot of juggling branches and commits around and will in the end break my running latexmk build.
Turns out this can be done elegantly with the filter functionality from git-attributes. With git filter you can automatically run two scripts: a smudge script when a file is checked out, and clean script when a file is checked in.
So in my case, I can define in .git/config
what I want to happen:
[filter "fix-texlive2019"]
smudge = sed '/^\\\\usepackage{amssymb}.*/d'
clean = sed '/^\\\\usepackage{booktabs}/i \\\\\\usepackage{amssymb}'
While the escaping-hell looks a bit odd, these sed commands achieve exactly what I need:
In my workspace, the \includepackage{amssymb}
line will be removed
(because it created a definition conflict with another package in my texlive version),
whereas during a check-in, the line will be inserted right before the
\usepackage{booktabs}
line, which is the place where it was before.
Now we just have to assign the filter to tex files, which is done in .git/info/attributes
:
*.tex filter=fix-texlive2019
Using this approach allows me to magically have my necessary changes, being able to compile the document at all times, without having to worry about accidentally committing the required local fix.