Managing generated files in GitHub

Bryan Clark
3 min readAug 22, 2018

Is this actually a large Pull Request or are you just looking at too many generated files? Are those generated files even reviewed?

You made it! That was a long diff to scroll through…

If you’re using GitHub and work with generated files in your repository you may be seeing large diffs and merge conflicts as you update your newly generated files. I’m going to show you how you can use the .gitattributes file in your repository to suppress the display and merge conflicts of generated files in code review, the command line, and the desktop app.

GitHub uses Linguist to understand and work with languages in repositories. So you can add Linguist options like linguist-generated=true to your .gitattributes file which will suppress the display of diffs in your pull requests.

Here’s an example .gitattributes file that will prevent any HTML or XML files in the root directory from being displayed in code review diffs

*.html    linguist-generated=true
*.xml linguist-generated=true
The same PR but now the generated files are hidden by default

As you can see the generated files are now collapsed by default, they can still be expanded if you want to look but you’ll be able to ignore them more easily in your code review.

If you’re a command line git user you can add this additional line to your .gitattributes file to prevent generated files from appearing when you run git diff.

git command line diff displaying generated files

The -diff option tells git to treat this file like a binary file. The diff output will indicate that the file has changed but won't display the differences. The -merge option tells Git to treat merging like a binary, too. It will accept the latest version and avoid any conflict resolution.

*.html    -diff -merge
*.html linguist-generated=true
*.xml -diff -merge
*.xml linguist-generated=true
git command line diff indicating generated files changed but not displaying them

So now you can see in the command line git lets you know that the files have changed but doesn’t show the contents. Also the merge option will prevent git from prompting you about merge conflicts which can happen with generated files. The linguist options are ignored by the git command line client.

And if you’re a GitHub Desktop user it will also will also recognize the command line .gitattributes changes and suppress diff view as well.

A .gitattributes file uses the same syntax as a .gitignore file for specifying individual files or types of files. Take a look at the docs on customizing how changed files appear, the Linguist README, and the .gitattributes documentation to learn more.

Hope this helps you!

--

--

Bryan Clark

Director of Product Management @Timescale (previously @GitHub, @Mozilla, and @RedHat)