What’s excluded from a Git repository

What’s excluded from a Git repository

Sometimes it’s not obvious what you should or shouldn’t include in your Git repositories. Luckily, there is a small set of ground rules to keep in mind while setting up your gitignore files, so you have better control of what shall or shall not pass!

Anything that gets generated

Anything that gets generated by a script, a build tool or tooling in general, should be excluded from the repository, as it’s basically part of the build process, not something to share with your team. Common examples include: CSS files generated by compiling Sass or Less files, minified JavaScript files, source maps etc.

Let’s explain why it is important to exclude those files from your repository now; Imagine a team of 2 developers working on a feature. Developer A, adds changes to a Sass file and so does Developer B. In case the generated CSS file is stored in the repository, there’s a high chance that the developers are going to face conflicts with each other. Now throw a minified CSS file to the mix and the situation gets even worse. Imagine solving a merge conflict in a minified CSS file?

Example addition in gitignore:


Anything that gets installed

JavaScript or backend libraries, 3rd party CSS stylesheets or anything that can basically be installed through a package manager, should not live in your repository, simply because it can be installed during the build process. After all, this is what package managers are for.

However, you need to include the package manager configuration file and any lock file that the package manager generates, for example package-json.lock, yarn.lock, Gemfile.lock etc

Example addition in gitignore:


Logs, output files, user generated content or whatever the app produces

Log files are ephemeral, meaning that they are often rotated, archived or deleted, which means that there’s a better place for them, either some directory on the server, a remote storage service or a remote logging service which integrates with your app.

Output files, for example if you’re building an invoicing tool and you generating PDF invoices, have no place in the Git repository, simply because they can cause immense trouble if their developments get mixed up or they get deleted from the production environment and same goes with the user generated content. The place for them is actually some remote storage service like Amazon Web Services S3 or DigitalOcean’s Spaces.

Example addition in gitignore:


Bonus tip: Keep a consistent directory structure, while ignoring directories

Sometimes though, in order for the app directory structure to be consistent, there’s the need to commit an empty directory in the application, but ignore its contents. How to do that you might ask; just add an empty .keep file which will remain hidden from you, but will enable Git to maintain the directory in the repository while ignoring its contents. For example, we need to exclude the “logs” directory, but we need to have it in the repository so here’s a sample command:

$ touch logs/.keep # create an empty file under the logs/ directory

Now, git has the option to negate ignored entries, meaning that you can ignore an entire directory structure but you can choose to exclude a few paths inside this directory from being ignored, so basically include them in the repository. Back to our example now, let’s commit the logs directory in the repository but exclude its contents, here’s how the gitignore entry would look like:


If you now run a git status command, you’ll see an untracked file to be added called logs/.keep