How to create a .gitignore file?
Tags: github, gitlab, programming
Published: 2022-12-29 07:51:20

---
title: How to create a .gitignore file?
published: true
description: Now that we know that having a .gitignore file will save a lot of disk space and network traffic, how to create one?
tags: github, gitlab, programming
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2022-12-29 06:12 +0000
---

Recently I published an article that was viewed by more than 13K people. I talked about the usefulness of having a `.gitignore` file in your git repositories.

{% embed https://dev.to/szabgab/billions-of-unnecessary-files-in-github-i85 %}

I gave some examples and pointed to the [documentation of .gitignore](https://git-scm.com/docs/gitignore) but I did not suggest how to create a `.gitignore` file. However, several people have commented with their suggestions. Let me summarize that.

## Create .gitignore manually

I personally usually create the `.gitignore` file manually. As my project progresses I run `git status` frequently. When I notice files in my working directory that are not tracked yet and **should not** be in git, I add the relevant patterns to `.gitignore`.

This has the advantage that I only add to `.gitignore` what is really needed in this project, but unfortunately sometimes I make a mistake and add some unnecessary files to git.

## Let GitHub create it

When you create a new repository on GitHub you need to fill a form giving the name of the repo and making some decisions. One of these decisions is to add a `.gitignore` file based on some templates.
They have explanations on [ignoring files](https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files). The templates themselves are stored in [this GitHub repository](https://github.com/github/gitignore).

(Though it seems the templates on the GitHub `new repo` page are not exactly the same as the ones in the repo I linked to. I [reported this](https://github.com/orgs/community/discussions/42821))

## Use gitignore.io by Toptal

If you already have a repository then you cannot use the above method. You could still copy the [template from the repository](https://github.com/github/gitignore), but there are other ways.

[gitignore.io](https://www.gitignore.io/) is an alternative way to generate the `.gitignore` file. It itself is an open source project. You can find its [source code](https://github.com/toptal/gitignore.io). It uses a separate [list of templates](https://github.com/toptal/gitignore). An interesting extra feature of this is that it can combine templates. So you could ask it to generate a template using `Python` and `JavaScript` and `HTML`. Well, you could, but I could not find the template neither for `JavaScript` nor for `HTML`. However I found one for `Node`. So we can combine `Python` with `Node`.

## gig a command line tool

Damien Sedgwick recommended the tool he wrote called `gig` which is written in Rust and uses the API provide by the gitignore.io. [Source of gig](https://github.com/damiensedgwick/gig).

{% embed https://dev.to/damiensedgwick/how-to-generate-a-blazing-fast-gitignore-file-2ieh %}

## Gitignore Online Generator

Another option is the [Gitignore Online Generator](https://gitignore.itranswarp.com/) created by Michael Liao. The [source code](https://github.com/michaelliao/gitignore-online-generator) shows that it is based on the [.gitignore templates by GitHub](https://github.com/github/gitignore). It also allows the combination of the templates.

Strangely it is a fork of the templates instead of a stand-alone project that would use the templates as an external resource.

## Let GitLab create it

In the alternate universes, in which you use [GitLab](https://gitlab.com/) when you create a new project you can decide to **Create from template** and then they will include a `gitignore` file in the repository they create. You can [view the templates](https://gitlab.com/gitlab-org/project-templates) and there are instruction on how to [contribute to built-in project templates](https://docs.gitlab.com/ee/development/project_templates.html). The list of templates is a lot more limited here than in the list of languages in the other cases, but they create a skeleton for a whole project not just the `.gitignore` file.

## .gitignore is a feature of git

Remember, `.gitignore` is a feature of git and not GitHub or GitLab, so the templates, the online and command line generators can be used regardless of the cloud-based git storage provider you use.

## Conclusion

There are 2 separate lists of templates for `.gitignore` files with several interfaces and there are the project templates by GitLab.