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.)
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.
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!
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.
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!
Top comments (2)
Check out Oh My Zsh - I, and a lot of other devs I know, use that, and the git aliases come as part of it! I'll do a more detailed post about it in the future, but it's definitely a time saver. 😁
If you are on a Mac, you'd want to add .DS_Store in a global ignore file.
If you allow dev-env specific files in your project's .gitignore, then next time a new Dev comes with a different environment (vscode vs webstorm vs him vs emacs vs whatever, Mac vs Windows, etc.) she'll want to add it there. Your gitignore is going to become an unmanageable mess.
Project gitignore files should only care about project specific rules (it's a JS project, is you're going to ignore node_modules, a Python project will ignore *.pyc files, a Maven one will ignore target directories, etc.)
GitHub's doc on gitignore is doing good in this regard.