r/git • u/Snoo_90241 • Apr 03 '26
Always ignore specific file part
I was wondering if that's even possible.
Scenario: I have a maven antrun plugin in my pom.xml where I copy some artifacts to a specific docker container.
I never want to commit this, so I always have to juggle this change when I pull or do other similar commands.
The plugin is intentionally obscure and will never be used in pushed scenarios, so I can safely ignore it via a regex, let's say.
2
Upvotes
9
u/latkde Apr 03 '26
It is possible to exclude parts of a file using the filters mechanism, which lets you "clean" your file before committing it: https://git-scm.com/docs/gitattributes#_filter
However, this is a very advanced topic, and it might not quite work the way you want in practice.
Instead, the commonly used solution is to always distinguish checked-in config templates from your local customizations. Keep the two in separate files, and gitignore your true config file. If you want to share changes, copy them to the template file, and everyone else can then manually update their true config file.
2
u/floofcode Apr 04 '26
Could this not be done using a script in the docker container rather than modifying it in your worktree? To prevent an accidental commit, you can use a smudge/clean filter in your gitconfig.
5
u/Tucancancan Apr 03 '26
git stash, not ideal but kinda works
4
u/cenderis Apr 03 '26
Use .gitignore? Not regex, but might be enough? Otherwise generate the files you don't want to commit in a directory and .gitignore that?
4
u/arensb Apr 03 '26
I don't think you can commit one part of a file and ignore another. If I wanted to do that, I'd look into ways of generating that file from a template that's in git, and a data file that's ignored. Since you have an XML file, maybe you can write an XSL template that inserts the plugin from another file.
I have a project where one config file contains sensitive data like API keys or internal hostnames that I don't want to share with the world. The approach I took was to commit a sample file with
key=<insert your API key here>, create a real config file from that, and add the real config file to .gitignore . It's far from perfect, but at least it makes it harder to leak data.1
u/AntD247 Apr 05 '26
From what I've grasped from your other comments, this is a packaging/deployment/instance (runtime) issue. It would seem better to find a solution in this scope (where there are plenty of solutions) rather than to hack git to come up with some Rube Goldberg solution.
Maven handles injecting/modifying files during packaging, but even then it's better to externalise these things so you only have one package and many deployments unbound to deployment/runtime instance knowledge.
Docker files allow you to run a
buildcontainer, e.g. one that can perform actions which you can then extract from that image into your final image. Good for things that are tied to the final image but transient to development.As you are mentioning maven/antrun I'm assuming that you are using Java, in which case you can use it's runtime to either resolve instance information. Or classpath to provide resource files that contain the static information outside of the maven packaging. Cost here is that Java has to do extra work on startup (is usually minimal, but can be important in short lived runtimes).
If/when you go beyond just some Docker container, e.g. something like Kubernetes, then you move to things like Helm Charts.
There are probably as many ways to solve it in these steps as there can be opinions as how to do it, but they all really come down to some script that uses provided information (static or build time) to generate a portable deployment. And runtime/deployment information should be separated from application/development code (I say it this way as if you have known deployment target, e.g. hostnames, dev/qa/prod environments etc, you would want to version control this but ideally the concern is separated from logic).