Generating a .gitignore for a Project
The company Toptal runs a wonderful api for composing a .gitignore on the fly.
By composing, I mean you give it a list of components that should be considered, and it generates a .gitignore snippet for each and concatenates them into one.
The api has a web interface at gitignore.io:

But also an api at https://www.toptal.com/developers/gitignore/api/ that can be interacted with from the command line:
$ curl -s https://www.toptal.com/developers/gitignore/api/
gitignore.io help:
list - lists the operating systems, programming languages and IDE input types
:types: - creates .gitignore files for types of operating systems, programming languages or IDEs
Let’s use the api to create some .gitignore files for a project.
MacOS
If the project will be developed on MacOS, let’s make sure to ignore the annoying files it generates automatically:
$ curl -s https://www.toptal.com/developers/gitignore/api/macos
This returns:
# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
# End of https://www.toptal.com/developers/gitignore/api/macos
Pretty neat! But you probably won’t just be concerned with ignoring MacOS generated files. Let’s say we are building a website using the static site builder called hugo and we want to include this as well.
MacOS + Hugo
$ curl -s https://www.toptal.com/developers/gitignore/api/macos,hugo
This returns:
# Created by https://www.toptal.com/developers/gitignore/api/macos,hugo
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,hugo
### Hugo ###
# Generated files by hugo
/public/
/resources/_gen/
/assets/jsconfig.json
hugo_stats.json
# Executable may be added to repository
hugo.exe
hugo.darwin
hugo.linux
# Temporary lock file while building
/.hugo_build.lock
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
# End of https://www.toptal.com/developers/gitignore/api/macos,hugo
Notice that the result is the composition of a MacOS snippet and a Hugo snippet! Pretty neat.
Okay, next, let’s say our hugo site will be making use of some Nodejs modules, and include a section for Node.
MacOS + Hugo + Node
$ curl -s https://www.toptal.com/developers/gitignore/api/macos,hugo,node
This returns:
# Created by https://www.toptal.com/developers/gitignore/api/macos,hugo,node
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,hugo,node
### Hugo ###
# Generated files by hugo
/public/
/resources/_gen/
/assets/jsconfig.json
hugo_stats.json
# Executable may be added to repository
hugo.exe
hugo.darwin
hugo.linux
# Temporary lock file while building
/.hugo_build.lock
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
### Node Patch ###
# Serverless Webpack directories
.webpack/
# Optional stylelint cache
# SvelteKit build / generate output
.svelte-kit
# End of https://www.toptal.com/developers/gitignore/api/macos,hugo,node
A file with a section for MacOS, Hugo, and Node!
Oh, let’s add Go, too, since it’s possible to add go modules to a Hugo site:
MacOS + Hugo + Node + Go
# Created by https://www.toptal.com/developers/gitignore/api/macos,hugo,go
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,hugo,go
### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
### Hugo ###
# Generated files by hugo
/public/
/resources/_gen/
/assets/jsconfig.json
hugo_stats.json
# Executable may be added to repository
hugo.exe
hugo.darwin
hugo.linux
# Temporary lock file while building
/.hugo_build.lock
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
# End of https://www.toptal.com/developers/gitignore/api/macos,hugo,go
That’s our completed file for an example project being programmed on a MacOS and using Hugo, Node, and Go!
Extras
Use gitignore.io domain instead
The domain https://gitignore.io runs a redirect service to https://toptal.com/developers/gitignore so the api can be accessed in a more memorable way. Just add –location/-L to the curl command to follow redirects.
$ curl -sI https://gitignore.io/api/
HTTP/2 301
date: Fri, 17 May 2024 19:16:12 GMT
content-type: text/html
content-length: 167
location: https://www.toptal.com/developers/gitignore/api/
cache-control: max-age=3600
expires: Fri, 17 May 2024 20:16:12 GMT
server: cloudflare
So, for instance, a Python .gitignore can be generated by running:
$ curl -sL https://gitignore.io/api/python
Shell function / Git alias
Finally, as another extra the api can be added as a shell function or git alias, as described in their documentation.
Let’s add a git alias:
$ git config --global alias.ignore \
'!gi() { curl -sL https://gitignore.io/api/$@ ;}; gi'
And use it to generate a Rust .gitignore:
$ git ignore rust,macos
Pretty slick, right!?