On the eigth day of Gitmas, my mentee asked of me… what’s this gitignore thing?
push-ing, pull-ing, diff-ing,
what is SSH-ing,
how do I connect these,
what’s this “GitHub” of which I’ve heard,
Git’s ready – what then,
how do I get Git set up,
and could you explain Git, pretty please?

Today’s Day 8 of the 12 Days of Gitmas, and today we’re talking about .gitignore – what is it, and when should you use it?

Right now, if you git add ., it will add every single file within your project to Git, and track all of them.

But sometimes, that’s not what you want. Say you have a huge log folder that’s generated when you run your project. Or, if you’re familiar with Node.js at all, that huuuuuge node_modules folder that’s created when you npm install. (If you have no idea what this means, no worries! This Git series is for absolute beginners, and Node.js is a bit more advanced.)

gif of santa being buried in letters

Suffice to say, there are a lot of cases where there are files that are created within your project that you won’t necessarily want to check in. But if you have a big project, it quickly becomes a huge pain to have to cherrypick which files you do want, and individually git add every single file.

This is where the .gitignore file comes in.

As you might guess from the name, .gitignore is a file that tells Git which files or folders to ignore.

This way, when you type git status in the command line, none of your ignored files will show up, or get added if you do git add . It’s a super easy way to tell Git what to pay attention to, and what to ignore.

What do you put in .gitignore? It’s just a text list of files, folders, and file extensions that you want to ignore.

screenshot of a sample .gitignore file

In my .gitignore file, I want the .DS_Store and ignoreme.txt files to stay out of my GitHub repository.

For example, if you’re working on a Node.js project, then you should probably add node_modules/ so it ignores the entire node_modules folder (which gets regenerated every time you npm install, so there’s no point in checking it in).

You can even ignore everything of a specific file type – so, if for some odd reason, you never ever want a JPEG image file to get checked in, you can add *.jpeg to your .gitignore file.

Now, when you type git status in your command line, the files or folders that you’ve ignored won’t show up – you won’t even have the option to add them or check them in, which is exactly what you want here!

screenshot of terminal showing that ignored files don't show up when you git status

You may notice that I typed gst instead of git status. That’s because I’m using aliased commands to make it easier when I’m using Git a lot! We’ll go over this in an upcoming post. The cat command just outputs the contents of the text file into the terminal.

And now, when you commit your changes and push them to GitHub, the ignored files won’t be there.

However! .gitignore only works for files or folders that haven’t been checked into your repository. If you created a README.md file and checked it into your repository, for example, and then added either README.md or *.md to your .gitignore file, future changes will still be tracked. If you want a file or folder to NOT be tracked, you’ll have to manually remove it from the repository and check the repository in without that file or folder.

screenshot of github after checking in the .gitignore file

New files won’t show up here if they’re supposed to be ignored.

Now you know all about .gitignore!

If you have any other questions about Git or requests for other topics you’d like to see me blog about, please leave a comment! And if you’re enjoying this series, consider supporting me on Patreon, following me on Twitter or checking out my coding or cooking Twitch streams!

Write A Comment