As a programming instructor, the number one mistake I see my students and graduates make when starting their side projects is that they forget to add a .gitignore
file. In this article we’ll discuss what a .gitignore
file is, how you set up a .gitignore
file, and why that’s important.
What is a .gitignore
File?
Put simply, a .gitignore
file is a text file that tells git to not track specific files, directories, or types of files.
For example, the following .gitignore
file tells git to ignore .suo
files, .dll
s , and everything in the node_modules
directory:
*.suo
*.dll
node_modules
Full .gitignore
files are much larger than this, but this gives you a good idea of what’s in a typical .gitignore
file.
If you have a .gitignore
file in your repository, git will ignore all matching files and folders when you attempt to add them.
Not every file in your programming projects is critically important and needs to be tracked by git. Many files are the specific results of compiling your code on one specific machine or operating system and should not be tracked by version control. For example, two people on two different operating systems may generate two different executable files from the same code. While the executable may work on one machine, it may cause serious problems for the other.
Additionally, many temporary files take up a significant amount of disk space and would make your git repository needlessly large.
Because of these reasons, we use a .gitignore
file to filter out unnecessary files as we check in our code. We then rely on the person who pulls our code to rebuild the code on their machine. Once they are done with their work, they commit their code changes and you’ll have those changes as well once you pull.
Because we do check in the .gitignore
file, all users will share the same rules for what files get checked in and what files get ignored.
How do you Create a .gitignore
File?
Now that we’ve discussed what a .gitignore
file is and why you would want one, let’s talk about how you get one.
Typically you get a .gitignore
file in one of two ways:
- You create the
.gitignore
file yourself and manually add in rules to the file - You start with a pre-created
.gitignore
file that matches the languages and technologies that you work with.
Creating a .gitignore
file is as easy as right clicking in File Explorer and choosing to create a new file, then naming it .gitignore
. You can also run the touch bash command to create the empty file with touch .gitignore
.
Important Note: .gitignore
files can go in any directory of your application and affect all nested directories. However, I recommend putting your .gitignore
file in your repository root directory for the most consistent results.
However, I almost never manually create a .gitignore
file. Instead, I start from a pre-existing template.
Using an existing .gitignore
Template
Most of the time I start my projects by using a pre-existing .gitignore
template.
One of the options that GitHub and other git providers give you when you create a new repository is to select a .gitignore
template from a list of supported ones as pictured below:
This will select a pre-built .gitignore
file for you to work with in your program. See GitHub’s documentation for more details about this feature and the languages supported in it.
Important Note: The correct .gitignore
template may not match the name of your language. For example, when writing C# code you might think your template name would be csharp
or even dotnet
when all dotnet languages share a single VisualStudio
template instead. Similarly, JavaScript is categorized under Node
instead of JavaScript
.
This feature significantly helps when creating new repositories, but what if you’ve already created your repository?
Thankfully, GitHub publishes a GitIgnore
repository that contains a variety of .gitignore
files for various languages. This is my default resource when I need to find a new .gitignore
file and don’t have a template to borrow from an existing project.
Adding to your .gitignore
File
Once you have a .gitignore
file you like, you may find instances where you want to customize the .gitignore
file for your specific project.
If you find particular files or folders that you want to add to .gitignore
files, just add the files by their name or path to the bottom of the .gitignore
file.
For example, to ignore a workspace.json
file in the .obsidian
directory and the contents of the _gen
subdirectory in the resources
directory, you’d add the following entries:
.obsidian/workspace.json
/resources/_gen/
See the git documentation for more information on the patterns and formatting supported in .gitignore
files.
Additionally, many graphical git tools like GitKraken will allow you to right click on files and add them to your .gitignore
file during commit.
Closing Thoughts
All told, .gitignore
files are a flexible and easy way to control what goes into your git repositories.
As a senior member of the technical community, I know a number of hiring managers who look for .gitignore
files in the side projects new developers have made. While the absence of a .gitignore
file is not a deal-breaker for them, the presence of one often shows the manager the potential hire is ready to write code professionally.
The moral of the story: if you’re working with git, have a .gitignore
file and your code will be more portable, contain less “junk” files, take up less space, and will feel more like professional code.
Top comments (1)
Thanks for this detailed article. Another tool to create specific .gitignore files is -> gitignore.io