Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

When .gitignore Stops Being Your Friend

DZone's Guide to

When .gitignore Stops Being Your Friend

How to troubleshoot your .gitignore rules that are a little too aggressive and keep your new project files from being picked up by Git.

· DevOps Zone
Free Resource

Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.

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:

Download the ‘Practical Blueprint to Continuous Delivery’ to learn how Automic Release Automation can help you begin or continue your company’s digital transformation.

Topics:
git ,visual studio

Published at DZone with permission of Douglas Rathbone, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}