Published on

Automatically generate .gitignore for your project

Authors

When starting a new project from scratch we might often forget to add a .gitignore file. And as a result, we might end up committing dependencies like node_modules (thousands of files) to the repository.

Or in the worst case, we might commit some credentials like API keys and publish it publically.

Ever wondered to create it automatically based on your needs?

That's where gitignore.io comes in.

How to generate a new .gitignore via GUI

  • Just head over to gitignore.io
  • Select the tech stack that you're using. (Select your dev OS also). For example, if you're working on a Next.js project then select "Next.js"
  • Click on Create and it'll generate the file's content.
  • Now, Copy and paste the content into your project's .gitignore file. If it doesn't exist, create one.

How to generate a new .gitignore via CLI

You can generate the file via CLI as well.

Follow their guide and configure it on your machine.

In my case, I prefer to have it as a bash function and use it easily anywhere

# .zshrc or .bashrc
gi() {
  curl -s "https://www.toptal.com/developers/gitignore/api/$*" > .gitignore
}

# Make sure to restart the terminal or "source" the zshrc or bashrc file like "source ~/.zshrc"

And to generate the file, run:

gi nextjs,macos

Happy ignoring files!