How to create good .gitignore file?
We, developers, use Git on a daily basis. And there’s one thing to Git that’s pretty important— its .gitignore file. It tells Git which files and directories should be ignored thus not committed to a repository.
Oftentimes it’s pretty obvious which files or directories should be excluded from being commited to a Git repository. If you use Node.js then node_modules is a no-brainer. We certainly don’t want to commit every of the project’s dependency since they can be easily fetched later thanks to package.json file. So exclude node_modules and we’re done worrying about unnecessary files that Node.js workflow may produce, right? Not exactly 😒.
WTF? — What the File? 😳
Did you know that using Node.js and its related tools like npm (or many popular packages) can produce a lot more files and directories that are often hidden? What about npm-debug.log file or .npm directory for cache?
And you probably use some kind of CSS preprocessor as well. Sass? It’s still doing great! And that tool can create .sass-cache directory. Again, not something we want to send over the wire when we push our code.
Let’s assume that we have this very simple workflow with Gulp and Sass and develop our app on macOS. And let’s say that we covered .gitignore rules for the first two above (there’s few more rules to it, but it doesn’t matter now). What about the operating system though? “You’re saying that it creates unwanted files too?” Unfortunately, yea. The most popular is probably .DS_Store file, that stores information about icons’ layout, order etc. Another could be Icon file that sits in a directory if you assign custom icon to it (I do it very often to quickly distinguish projects I’m working on). Windows OS is no different on that matter. It can create Thumbs.db or Desktop.ini files that you also don’t want to commit.
So how can you possibly know every single file or directory that can be produced by third-party tools yet is not relevant to your source code? The truth is, you can’t. That’s why GitHub offers you a collection of predefined .gitignore files for pretty much every popular tool, language or platform. You can mix them to cover everything you use for a project. Another way is to use great online tool gitignore.io that helps you do the same in one step. You can choose a tool or language and what you get back is ready to be used .gitignore file that covers exclusions for those.
“I’m in lazy mode, dude” 😎
What if you’re like me and you don’t want to leave your code editor if it’s not necessary? The good news is that you don’t have to if your editor of choice is Visual Studio Code. I’m a recent convert to that editor after using Sublime Text for over 6 years. I created extension for it that leverages gitignore.io API.
You may ask “what does it do?”. It’s simple. You run provided command, select tools, languages or operating system, hit Enter and you’re done — .gitignore file lands in your project’s directory (or shows up as an untitled file if you don’t have any folder opened).
And then it goes even further. If you realize that you forgot to choose something or you just want to add any other rules you simply run the command again. It automatically detects tools that you already have chosen and lets you add some more or deselect any of the current ones.
Next question that may just popped into your head is if you can add some custom rules to the generated .gitignore file without loosing them on next update if you decide to modify the rulset? Of course you can! Everything that you add at the bottom of the file will be preserved upon next update.
Sound like something you may find useful? Feel free to try it out and read its description page for usage and more information here: .gitignore Generator for Visual Studio Code.
And if you have any suggestions on how can I make it better, just let me know. You can reach me on Twitter.
Oh, and if you did find it useful, rating and review would be much appreciated! 🙏
Thanks!