Advent Day 10: Add a .gitignore

December 10, 2018

This is day 10 of my Git Tips and Tricks Advent Calendar. If you want to see the whole list of tips as they're published, see the index.

You need to be mindful of the files that you're putting into your repository; and, perhaps more importantly, the ones that you're keeping out of it. You don't want to check in local configuration settings for your IDE that affect the rest of your team. You don't want to check in editor temporary or swap files that will just give you pointless merge conflicts. And you definitely don't want to check in private keys or cloud provider secrets.

To set this up, create a file named .gitignore at the root of your repository, and list the files, folders or wildcards that you want to ignore. For example, if you use vim and you want to make sure that you don't accidentally check in your swapfiles, your .gitignore might look like:

.*.swp

And if you work on C projects, your .gitignore might look like:

*.o
*.a
*.exe

Thankfully, you don't have to think too hard about this, since GitHub maintains a collection of .gitignore files that are tuned for the specific languages and IDEs that you use. For example, if you're writing Python, you can add the Python-focused .gitignore file to your repository and you won't have to worry about accidentally adding the __pycache__ directory to your repository ever again.

But you have to remember to set this up when you first create your repository; .gitignore doesn't apply to files that are already in the repository.

Too late? Did you add some files that you want to remove and start ignoring? Tune in tomorrow.