Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle tracked generated files in git?

Tags:

git

gulp

githooks

Background:

We have generated files in our system that need to be committed in their final state (e.g. we can't rely the source (ungenerated) files and make the generated ones on the fly). Since we need the generated files, those files cannot be git ignored.

Problem:

The files trigger a lot of Your local changes to the following files would be overwritten by merge errors from git on checkout and pull commands.

When I pull or checkout, I never care what my files were, I only care about the new files. (Obviously I care a lot about the source file that makes the generated files. I'm happy for merge warnings on the source file, which is kept in a different directory.)

However, when I commit, I want my version to "win" and my generated files to be committed.

Possible solution:

Right now I just run git checkout -- generated-files/ before a pull or checkout to reset my generated files and skip any merge errors. It works, but I often forget to do it, and I'd like to automate it if possible.

I looked into a pre-checkout and pre-pull hook, but git doesn't provide them :(

Questions:

  • Is there a better way to deal with generated files?
  • Is there a way to force the git checkout -- generated-files/ to run before a pull or checkout?
  • How do you deal with generated files in git?
like image 519
Lucy Bain Avatar asked Dec 23 '15 05:12

Lucy Bain


People also ask

What are tracked files in git?

Tracked files are files that were in the last snapshot, as well as any newly staged files; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.

How do I ignore a .cache file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.


1 Answers

My personal mantra: if the file can be generated, only the parts that can generate it belong under source control. Otherwise, you wind up with a lot of the noise that you're encountering right now.

If the generated files are a genuine, recurring, repeatable step and part of your build chain, it is safe to add the directory they live under to your .gitignore file (and don't forget to git rm --cached the folder so they're not tracked anymore).

If the generated files are part of the actual source code, then a conversation should happen between members of the team and/or leads, and discuss why these files are under revision if their generation is automated. There may be legitimate reason to keep tabs on them, and there may not be - it's worth the ask, at least.

My recommendation is to ignore any files that were generated and keep them out of Git. But, you'll want to talk to your teammates to see if that's the convention everyone wants to adhere to.

like image 144
Makoto Avatar answered Oct 09 '22 04:10

Makoto