Optimize tracking generated files in Git

While it is generally inadvisable to track files generated from other files in a Git repository, sometimes people still have to do so for other reasons. For example, it is advised to track some files generated by package managers such as package-lock.json generated by npm and Gemfile.lock generated by Ruby Bundler. For another example, a file may take a long time to generate but is for some reason heavily integrated with the repository.

When it is necessary to track some generated file, Git by default display the changes in the (textual) generated file in the output of commands such as git show, git diff, etc. The changes in the generated file in the output of those commands are not interesting and we usually would prefer hiding them. We can hide them by marking the file as binary in the .gitattributes file at the root of the repository. For example, to suppress Git from displaying changes in package-lock.json, we can add the following text to the .gitattributes file (create this file if it doesn’t already exist):

package-lock.json binary