12 Days of Gitmas: .gitignore
Tags: git, github, beginners, tutorial
Published: 2020-01-09 07:06:14

*On the eigth day of Gitmas, my mentee asked of me...
__what's this gitignore thing?__
[push-ing, pull-ing, diff-ing](https://dev.to/sushicodes/12-days-of-gitmas-committing-your-changes-in-git-53nk),
[what is SSH-ing](https://dev.to/sushicodes/12-days-of-gitmas-ssh-vs-https-19bp),
[how do I connect these](https://dev.to/sushicodes/12-days-of-gitmas-connecting-your-local-repository-to-github-1d9h),
[what's this "GitHub" of which I've heard](https://dev.to/sushicodes/12-days-of-gitmas-what-is-github-11po),
[Git's ready - what then](https://dev.to/sushicodes/12-days-of-gitmas-your-first-git-project-g3m),
[how do I get Git set up](https://dev.to/sushicodes/12-days-of-gitmas-setting-up-git-39i8),
[and could you explain Git, pretty please?](https://dev.to/sushicodes/12-days-of-Gitmas-intro-to-Git-4l26)*

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](https://media.giphy.com/media/3o7522WMYYkseyBo1G/giphy.gif "Too many files!")

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](https://live.staticflickr.com/65535/49354784751_148860b489_o.png "Here are the files you want ignored!")
<figcaption>In my .gitignore file, I want the <code>.DS_Store</code> and <code>ignoreme.txt</code> files to stay out of my GitHub repository.</figcaption>

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](https://live.staticflickr.com/65535/49354334573_09fb1d2d2d_b.jpg "No ignoreme.txt file!")
<figcaption>You may notice that I typed <code>gst</code> instead of <code>git status</code>.  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 <code>cat</code> command just outputs the contents of the text file into the terminal.</figcaption>

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](https://live.staticflickr.com/65535/49354336158_b40e4e98b7_b.jpg "GitHub after .gitignore")
<figcaption>New files won't show up here if they're supposed to be ignored.</figcaption>

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](https://www.patreon.com/sushiday), following me on [Twitter](https://twitter.com/sushiday) or checking out my [coding](https://www.twitch.tv/sushicodes) or [cooking](https://www.twitch.tv/sushiday) Twitch streams!