DEV Community

Shi Han
Shi Han

Posted on

gig: a gitignore generator

If you are using git in your project, you probably know what is .gitignore. Since I never seem to be able to memorized gitignore's pattern format correctly, when I needed to write a gitignore file for a specific project, I'll visit github/gitignore in my web browser, search for gitignore file, open the file, copy its content, then finally paste it into my editor...

Using github/gitignore

As we can imagine, if you need to create gitignore file for multiple programming languages the process becomes very tedious.

A while back I was introduced to gitignore.io which is a web service that allows us to create gitignore file easily for multiple languages. gitignore.io also provides API access and very nice documentation on how to run gitignore.io from your command line. It was a huge upgrade and I've been using this tool since then. I even submitted a PR to gitignore direnv:

Add direnv.gitignore #241

Based on https://github.com/direnv/direnv/wiki/Git

Pull Request

Thank you for contributing to @dvcs/gitignore and https://www.gitignore.io.

New or update

Select the appropriate check box for this pull request. This helps when merging to ensure there are no conflicts with other templates or misunderstandings of how thee template list works.

New

  • [x] Template - New .gitignore template
  • [ ] Composition - Template made from smaller templates
  • [ ] Inheritance - Template similar to an existing template
  • [ ] Patch - Template extending functionality of existing template

Update

  • [ ] Template - Update existing .gitignore template

Details

direnv is a tool that can load and unload environment variables based on the current working directory. It is commonly advised to not to include its configuration file .envrc into a public repository therefore having this in the gitignore will be very handy. The values are based on this documentation: https://github.com/direnv/direnv/wiki/Git

Thank you.

However, after the PR got merged, I found out that the change is not reflected in gitignore.io (yet). My guess is that the maintainer needs to "redeploy" the site in order to include the latest collections.



$ curl https://gitignore.io/api/direnv

# Created by https://www.gitignore.io/api/direnv
# Edit at https://www.gitignore.io/?templates=direnv

#!! ERROR: direnv is undefined. Use list command to see defined gitignore types !!#

# End of https://www.gitignore.io/api/direnv


Enter fullscreen mode Exit fullscreen mode

gitignore.io's collections of gitignore templates are maintained in a separated repository: github.com/toptal/gitignore. They are extensions of github/gitignore that include additional types known as .patch, .stack. When I bumped into the error above, I wondered if I can somehow generate the gitignore file using the repository directly without the web service. Which then lead me deep into the rabbit hole and created gig...

GitHub logo shihanng / gig

Generate .gitignore files from your terminal (mostly) offline!

gig -- .gitignore generator

GitHub GitHub release (latest by date) Go Report Card Coverage Status

gig is a command line tool to help you create useful .gitignore files for your project It is inspired by gitignore.io and make use of the large collection of useful .gitignore templates of the web service This also means that gig supports the are four file types that gitignore.io recognizes. Content generated by gig should match the one generated by gitignore.io except the order of stacks in which gitignore.io does not seem to guarantee any.

Motivation

Prior to this project, I used to have one of these command lines in my .zshrc. However, problems I have with this command line are that

Therefore this tool is created to solve…

Introducing gig

gig is a CLI port of gitignore.io and make use of the large collection of .gitignore templates of gitignore.io mentioned above. This also means that gig supports all four file types that gitignore.io recognizes and generates contents of gitignore like gitignore.io does.

Installing

If you are on macOS and are using Homebrew,



brew install shihanng/gig/gig


Enter fullscreen mode Exit fullscreen mode

For other platforms, download the pre-built binaries from the release page then place the binary in the $PATH e.g. /usr/local/bin.

Using

The most direct way of using gig is to use the gen subcommand with a list of template names as arguments, e.g.



gig gen go terraform > .gitignore


Enter fullscreen mode Exit fullscreen mode

Example above generates gitignore templates for Go and Terraform then redirects those into .gitignore file. With additional -f flag, we can omit the redirect part of the command. gig command will cache the content of github.com/toptal/gitignore repository into $XDG_CACHE_HOME/gig during the first run. This means that the next execute should work offline.

When we want to look for available templates, we can use the list command. It's a pretty long list, so it works better with a pager:



gig list | less


Enter fullscreen mode Exit fullscreen mode

There is also a search command which calls fzf -m internally on the list above and allows user to select (by pressing tab by default) the templates in fzf.

Finally there is the least useful autogen subcommand which was added just for fun.



gig autogen


Enter fullscreen mode Exit fullscreen mode

It will look into the current directory and try to figure out what to gitignore. Internally it uses source{d}'s enry package for programming language detection (which itself is a port of GitHub's linguist). Unfortunately it does not always work as expected:

  • It is not able to detect framework, e.g. .js files will be detected as javascript but there is not JavaScript gitignore template available.
  • go.mod and go.sum files will be detected as text for the reason mentioned in this PR.

gig started as a fun project to see how far I can go to build a CLI tool with the remix of

Now it is has become a tool that I will use it when starting a new project.

Please give it a try and I would love hear your feedback (via issues/PRs in project repository or comments in this post). If you like it, give it a ⭐ so that other people will hear about it.

Thanks for reading ❤️

Top comments (5)

Collapse
 
vlasales profile image
Vlastimil Pospichal

Global .gitignore

curl -s "https://www.gitignore.io/api/vim" -o ~/.gitignore
git config --global core.excludesfile ~/.gitignore
Collapse
 
vlasales profile image
Vlastimil Pospichal

This is not good for make .gitignore, but it is good for make .git/info/exclude.

Collapse
 
shihanng profile image
Shi Han

I have to admit I did not know about about /info/exclude until just now. Thanks!

Collapse
 
padakipavan profile image
padaki-pavan

This is great. Would love to use this as a vscode extension though. Keep up the good worth.

Collapse
 
shihanng profile image
Shi Han

Thank you for the suggestion!