git-question: Ignoring files on developer machines that are actually in the repo

Hello fellow git-users,

how can I solve the following situation:

The repository contains the following directories (with respective files inside):





On the developers machine code is put into the _gen-directories, but that code should not be committed from those machines. As the code the should go into the repositories for these directories will generated and updated through the CI process.

Is there a way to allow the CI machine to commit into these directories while on the developer machines code updated there should just be ignored?

The repository wide .gitignore does not help for.

The .git/info/exclude does not seem to help, as files in the _gen-dirs will get committed on modify/delete, while added files are ignored.

Also a global gitignore does not solve my problem.

Any idea would be appreciated a lot!


You could use a pre-commit Git hook to:

  1. Check to see if you’re on a CI machine (most CI providers have a CI=true environment variable set specifically for this purpose
  2. If not running on a CI machine, check to see if the commit contains changes to the gen directories, if so then fail with an appropriate error message

The problem with client side hooks like the pre-commit hook is that they’re not copied when cloning a repository, meaning you have to install them some other way on every machine. Most often it is recommended for enforcing a policy like this one that you do it in one of the server-side hooks like pre-receive. The problem with that approach is that someone might be pushing 50 commits and only one of them has a bad change, it would be best to catch something like this sooner.

One of those approaches is how I would do it, you’ll probably need to evaluate the tradeoffs for yourself and your team’s specific use case.

I hope that helps!

Hello lee-dohm,

thanks a lot for your answer! 

Unfortunately it does not solve my problem (which I not fully described in my initial post):

The developers are allowed to make modifications inside the ‘gen’ directories (usually with locally generated code), but those modifications should always be ignored (or can be discarded) when committing and also when pulling. So just failing the commit when there are changes is not sufficient. 

So the “obvious” solution on the developer-side would be to always discard all local changes in the ‘gen’-directories when committing (could be done using a pre-commit-Hook as you proposed) and pulling, but it seems there is no hook for the pull event?

Thanks again

It sounds then that what you’re really trying to do is generate build assets on the build machines that aren’t going to be used anywhere in the development or build process. Because of this, I wouldn’t recommend storing them in the repository at all. This gets around the whole “how do I conditionally ignore something?” problem. Depending on how you’re doing things, you could have the build machines upload the generated build assets to S3, some internal server, or what-have-you. This is the thinking behind the GitHub Releases feature.