When .gitignore stops being your friend - Debugging missing Git repository files

comments

One of the first things you figure out when working with Git is that like other source control providers, you want an easy way to exclude files from ending up on your source control to save yourself uploading items such as binaries and local user files (here’s looking at you Resharper!). By adding a .gitignore file to your repository you easily make this possible (if you’re using Windows, feel free to take a look at my post on creating these files successfully in Windows). The problem you have next is when you add a gitignore rule that’s a little too aggressive and your new project files fail to be picked up by Git, but you’re not quite sure which rule is the cause – this post shows you how you can troubleshoot these issues.

When you exclude too much

Let’s take a sample (overly aggressive) Visual Studio .gitignore file:

*.user
*.suo
*.dll
*.pdb

You’ll notice that the above file excludes all of your binary files. This is a pretty common occurrence; you want to avoid compiled files making their way into your repository if you can.

The problem arises when you have actual *.dll files that you need to add to your project as references.

I have a folder structure that looks like this:


/lib/Debug/[debug binaries and pdbs]
/lib/Release/[release binaries and pdbs]
/src/Website/*.*
/src/Service/*.*
..

If I use the .gitignore file above some of the binaries in my dependency folders “/lib” wont being added to the repository.

I could troubleshoot this a bunch of ways, the simplest being deleting lines of my ignore file until the files show up in my unstaged list. If you’re like me you might have a 30 line ignore file and this can be painful.

Luckily Git has a simple tool to help make this easier: the check-ignore command. This executable allows you to enter a file name and see which gitignore file and what line of it your file is being excluded by.

Debugging walkthrough

Open up a terminal session, or Bash console in Windows.

Move to your repository directory.

cd /c/code/myproject

Then call the check-ignore file against the file that’s missing from your repository.

git check-ignore –n –v lib/Debug/my-dependency.dll

What will be returned is the name of the file that your ignore rule is stored in, and the line.

.gitignore:45  "lib/Debug/my-dependency.dll"

Note that the above error says that the exclusion is on line 45 of my local .gitignore file.

It’s that easy.

The interesting part about this is that Git has multiple .gitignore files, and the above may actually show you that the file containing the rule that’s excluding your files. If you end up with a result like the one below you’ve discovered another hard to find issue:

"C:\\Users\\Doug\\Documents\\gitignore_global.txt":21:[Dd]ebug/     lib/Debug/my-dependency.dll