Git feature icon

Git has a handy feature when it comes to preventing accidental file check-ins when the files are meant to stay local. The obvious candidates are compiled binaries when you only want to check in the source code. Other candidates are files with local configurations.

One can put all of those files and paths into a .gitignore file in the project. To persist those changes (and to share the common file contents with collaborators on the project), one usually adds the .gitignore file to Git like any other file in the project.

The problem

Unfortunately, there are limits to this approach.

Putting local files into the .gitignore file only works well with items that are common for all collaborators, such as files and directories within the project. Just imagine what would happen if hundreds of collaborators put their specific paths into .gitignore. That practice would create a huge mess and churn.

Adding file names and paths can also reveal information that should not be public. It could, for example, reveal customer information (this issue is not only about file contents, but also about the customer's name, which can reveal information as metadata).

Just not checking in the .gitignore file can be a pain as well. When one is switching branches or updating the local working tree, one has to often stash the file (with local changes), switch the branch, or update and then unstash (potentially with merge conflicts).

Help is available

Luckily, Git offers alternative ways to prevent accidental file check-ins. For example, the file .git/info/exclude works just like .gitignore on a per-project base. If you need to ignore certain file patterns (e.g., backup files for an exotic editor), you can even use a per-user file like ~/.config/git/ignore. The cool thing is that these files live in areas that Git does not examine. Git will thus not add them to the changeset, so it will not commit and push to a remote.

Note: These other two files use the .gitignoreformat, so you can use wildcards in those as well.

Listing ignored files

Git would not be Git if it did not have commands that help you determine if a file or directory is ignored. The first of these commands is git ls-files:

$ git ls-files --others --exclude-from=.gitignore
$ git ls-files --others --exclude-from=.git/info/exclude

The --others argument tells the command to show files that are not in the index, and the --exclude-from is a filter to not show files from its parameter. Thus, the first version shows thus the ignored files that are not listed in the .gitignore file.

Another helpful command is git check-ignore, which requires a path argument. It returns the file name and an exit code of 0 on success. Otherwise, this command exits with a code of 1 if the argument is not in one of the ignore files.

Git has a large set of manual pages that can help you with the commands and files. Most notably for our purpose, you will want to focus on the gitignore(5) page.

 

Last updated: October 26, 2023