How to create a .gitignore file?

Gabor Szabo - Dec 29 '22 - - Dev Community

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.

I gave some examples and pointed to the documentation of .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. The templates themselves are stored in this GitHub repository.

(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)

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, but there are other ways.

gitignore.io is an alternative way to generate the .gitignore file. It itself is an open source project. You can find its source code. It uses a separate list of templates. 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.

Gitignore Online Generator

Another option is the Gitignore Online Generator created by Michael Liao. The source code shows that it is based on the .gitignore templates by GitHub. 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 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 and there are instruction on how to contribute to built-in project templates. 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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .