Reputation: 9035
Our build process automatically generates a lot of source code files that don't follow any particular naming convention. One thing the do all have in common is a comment that says "(Autogenerated)" inside the files. The problem is since these files are automatically generated by the build and never modified by hand, I don't want to check them into source control.
As far as I can tell there is no simple way to add these files to .gitignore
because they don't follow any particular naming convention. Adding all of them manually is also not an option because there are hundreds of them, and more could appear in the future. Is there any way to make git ignore all files that contain the word "(Autogenerated)" inside of the file?
Upvotes: 1
Views: 736
Reputation: 45659
There is not a way in git to create content-based ignore rules. You can sort of approximate it.
You could use a pre-commit hook to check for files containing the pattern ((Autogenerated)
), such as perhaps by
for file in $(git grep -l --cached '(Autogenerated)') ; do
and remove them from the index if found
git rm --cached $file
done
Of course it would be nice if you could insulate yourself better from false positives. Even a partial filename/path pattern to limit what git grep
looks for would help.
If you expect (or find) false positives to be a problem you can't avoid, you might want to have the hook print out a recommended list of files to unstage and then reject the commit, instead of unstaging the files and continuing. But then you'll need a way for the user to "whitelist" the files they want committed, and the hook would have to check against that.
The thing about client-side hooks (like pre-commit) is, you have to individually set them up on each clone. If someone forgets, then they will be checking in generated files unexpectedly.
Also, over the course of build-and-commit cycles, users will accumulate untracked (but not ignored) files in their worktree, which could at some times interfere with what they want to do.
It would be better if you could modify your build process to generate these files outside the worktree (or at least at a consistent path you could ignore).
Upvotes: 2