Do you have a .gitignore template setup in your text expander or code editor snippets? Do you tediously add coverage to it over time as you find new files you don’t want committed into your Git repos? Well, now that you’ve spent all that time on it, get ready to let that old thing collect some dust. We’re going to take things to the next level with gitignore.io.
I haven’t met many developers who were aware of gitignore.io yet. gitignore.io is a web service designed to help you create .gitignore files for your Git repositories. It is super easy to setup. You’ll never look back once you’ve integrated it into your workflow.
Using The gitignore.io CLI
If you’re like me you’re going to want to setup and use gitignore.io via its command line interface. For my workflow, having gitignore.io available right in Mac OSX Terminal is great. It only takes a minute or two to put into place. So let’s dive in.
Setting Up gitignore.io In Mac OSX Terminal
A couple of important things to note first:
- I use ohmyzsh (ZSH) instead of Terminal’s default Bash shell, but this has little impact on gitignore.io setup. I’ll point out the difference when it is time. I got you covered.
- I use iTerm2 instead of vanilla Terminal. This makes no difference where gitignore.io is concerned. So you can follow the same steps regardless of which one you use.
Step 1 – Creating A Function In Your Shell
The first thing you need to do to use gitignore.io on the CLI is add a function to your shell. This is true regardless of whether you use ohmyzsh or Bash. The only difference is a small part of the function you’ll create.
Write Straight To Your Shell Profile
If you just found yourself saying WTF dotfiles? I’ll probably go into them a bit more in a future blog post. For now you don’t have to worry about them. You can create a function in your shell using the following:
For ohmyzsh:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >> ~/.zshrc && source ~/.zshrc |
For Bash:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ echo "function gi() { curl -L -s https://www.gitignore.io/api/\$@ ;}" >> ~/.bash_profile && source ~/.bash_profile |
I’ll explain what this is doing without going too deep into Shell scripting. This command writes (echo) a function called gi() to your shell profile. It then reloads your shell so that the function is available and ready to use.
You can see from the commands for ohmyzsh and Bash that the function is exactly the same. The only difference in the commands is the Shell profile file you write to and reload. That is .zshrc for ohmyzsh and .base_profile for Bash.
dotfiles Version
I create and maintain my own custom dotfiles. If you do as well then this section is for you. If you already used the command above you can skip on to Step 2.
I use gi() already for some other functionality. So I instead created a function called ignore(). You can name this function whatever you want. Here is what my function looks like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate .gitignore file from gitignore.io API | |
# $@ – Comma separated list of templates to use, i.e. osx,sublimetext | |
# Common commands: | |
# 'ignore list' – List all currently supported templates | |
# 'ignore example1,example2' – Preview output in console | |
# 'ignore example1,example2 >> .gitignore' – Output to .gitignore file in CWD | |
# 'ignore example1,example2 >> ~/.gitignore' – Output to global .gitignore | |
function ignore() { | |
curl -s https://www.gitignore.io/api/$@ ; | |
} |
Like any function I write, this one comes well commented. I’ve also provided some usage examples.
Step 2 – Test Your Function
Remember when I told you setup was easy. I didn’t lie. Step 1 was all the setup required. But now you should test the function you created to make sure it works. If you know Shell script you already know what this function does. I’ll explain it just in case you don’t.
The function takes a single argument which is a comma separated value (CSV) of supported templates (more on supported templates later). It then makes a cURL request to the gitignore.io API. By default, the cURL request will output its results in Terminal.
That is handy for testing that the function works. You get to preview the result, which, will let you know if the function is working. You’ll also get to see what the function returns based on the templates you passed it. This is perfect for seeing what will go into your .gitignore file as well.
Now for the test. In terminal enter the following command (replacing “ignore” with whatever you called your function):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ignore osx |
You should see the something like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Created by https://www.gitignore.io | |
### OSX ### | |
.DS_Store | |
.AppleDouble | |
.LSOverride | |
# Icon must end with two \r | |
Icon | |
# Thumbnails | |
._* | |
# Files that might appear on external disk | |
.Spotlight-V100 | |
.Trashes | |
# Directories potentially created on remote AFP share | |
.AppleDB | |
.AppleDesktop | |
Network Trash Folder | |
Temporary Items | |
.apdisk |
If you don’t see anything or you see errors please leave a comment and I’ll try and help you out. If your function is working then you can move on to Step 3.
Step 3 – Creating Your First .gitignore File With gitignore.io
To create your first .gitignore file just rerun the same command you used in Step 2. Only this time you’ll write the results into a .gitignore file. If you’re just learning CLI then realize that whenever you use “>” after a command and then provide a path/to/filename it will create a file in that location. If you just use the filename it will create the file in your current working directory (CWD).
Note: Before you enter the command make sure you’re okay with creating a .gitignore file in your CWD. You can also use the path/to/filename to specify another location to create the file if you don’t want to change your CWD.
So the command is:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ignore osx > .gitignore # or path/to/.gitignore |
Setting Up gitignore.io On Linux Or Windows
If you’re on either Linux or Windows you’ll be happy to know they support gitignore.io as well. Setup is slightly different, but still pretty easy. I recommend you head over the gitignore.io docs and follow the steps there.
Using The gitignore.io GUI
If you prefer to use a GUI, no worries. You can skip the CLI setup. You can instead copy and paste a template into your .gitignore file using gitignore.io itself. The home page is the GUI.
gitignore.io Supported Templates
I promised you I’d say more about supported templates. To use gitignore.io to its full potential, you’ll need to know what templates it supports. Otherwise, you may find yourself facepalming as you try to find the correct argument to pass your function.
Finding Supported Templates
You can find this using any of the following methods:
1) Use the list argument:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ignore list |
3) AutoSuggest GUI on gitignore.io
My Most Used Template Combo
If you’re wondering what combination of templates I used the most, wait no longer! Here is my most used CLI command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ignore osx,windows,linux,sublimetext,eclipse,node,grunt,bower,sass,jetbrains > .gitignore |
I use Sublime Text for almost everything. I also do a lot of work with Node.js, Grunt (I use Gulp a lot more now, but it isn’t supported yet), Bower and Sass. Some of the other engineers I work with use either Eclipse or JetBrains on Windows. This command does a pretty good job of covering all bases for most of the projects I work on.
Conclusion
The ease of setup alone should be a major selling point for you. Consistency is another great aspect that gitignore.io offers. It definitely does a great job of keeping the most common ignored files out of your repos.
Still it does have a few improvements I’d like to see. I’d love to see instructions for contributing new templates to the project (Gulp please!). I’d also love to see some version awareness. Having an easy way to check if the templates changed since you’ve created your .gitignore file would be nice.
I’m interested in hearing about your own experiences with gitignore.io. Please share your comments and thoughts below. Thanks for reading!
Leave a Reply