DEV Community

Cover image for Gitignore Revisited
Chuck
Chuck

Posted on

Gitignore Revisited

I wanted to take the time to revisit the idea of a thorough gitignore file for your project repository. I few months ago I wrote an article about an extension to generate a gitignore file (Gitignore in VSCODE, but I am no longer using VS Code, and I have found a solution which everyone may find helpful.

Setup

Recently I have become sold on the gitignore api. This API is relatively easy to setup and full featured for fine tuning your needs.

Setup: It is pretty easy to start with, and you can read more in their docs. The documentation in this article will use ZSH as it is by shell of choice, but more configuration options are available on their site.

Add the following to your .zshrc file:

echo "function gi() { curl -sLw "\n" https://www.gitignore.io/api/\$@ ;}" >> \
~/.zshrc && source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Getting Output

So now what? Consider this sample command to get started for a java project on linux:

gi linux,java >> ./.gitignore
Enter fullscreen mode Exit fullscreen mode

This will create a file that will generate gitignore which will exclude the normal temporary files for Linux and Java that you may not want in your repository.

Question: What is enough to include in this file? Are you pushing your project to a public repository? If so, we may want a more complete gitignore file to keep the repository clean.

The command gi list will show us all the possible configuration options.

1c,1c-bitrix,a-frame,actionscript,ada
adobe,advancedinstaller,adventuregamestudio,agda,al
alteraquartusii,altium,android,androidstudio,angular
anjuta,ansible,apachecordova,apachehadoop,appbuilder
appceleratortitanium,appcode,appcode+all,appcode+iml,appengine
aptanastudio,arcanist,archive,archives,archlinuxpackages
aspnetcore,assembler,ate,atmelstudio,ats
audio,automationstudio,autotools,autotools+strict,awr
backup,ballerina,basercms,basic,batch
bazaar,bazel,bitrise,bitrix,bittorrent
blackbox,bloop,bluej,bookdown,bower
bricxcc,buck,c,c++,cake
cakephp,cakephp2,cakephp3,calabash,carthage
certificates,ceylon,cfwheels,chefcookbook,chocolatey
clean,clion,clion+all,clion+iml,clojure
cloud9,cmake,cocoapods,cocos2dx,cocoscreator
code,code-java,codeblocks,codecomposerstudio,codeigniter
codeio,codekit,codesniffer,coffeescript,commonlisp
composer,compressed,compressedarchive,compression,concrete5
coq,cordova,craftcms,crashlytics,crbasic
crossbar,crystal,csharp,cuda,cvs
d,dart,darteditor,data,database
datarecovery,dbeaver,defold,delphi,dframe
diff,diskimage,django,dm,docfx
docpress,dotenv,dotfilessh,dotnetcore,dotsettings
dreamweaver,dropbox,drupal,drupal7,drupal8
eagle,easybook,eclipse,eiffelstudio,elasticbeanstalk
elisp,elixir,elm,emacs,ember
ensime,episerver,erlang,espresso,executable
exercism,expressionengine,extjs,fancy,fastlane
finale,firebase,flashbuilder,flask,flex
flexbuilder,floobits,flutter,font,fontforge
forcedotcom,forgegradle,fortran,freepascal,fsharp
fuelphp,fusetools,games,gcov,genero4gl
geth,ggts,gis,git,gitbook
go,godot,goodsync,gpg,gradle
grails,greenfoot,grunt,gwt,haskell
helm,hexo,hol,homeassistant,hsp
hugo,hyperledgercomposer,iar,iar_ewarm,iarembeddedworkbench
idapro,idris,igorpro,images,infer
inforcms,inforcrm,intellij,intellij+all,intellij+iml
ionic3,jabref,java,java-web,jboss
jboss-4-2-3-ga,jboss-6-x,jboss4,jboss6,jdeveloper
jekyll,jenv,jetbrains,jetbrains+all,jetbrains+iml
jgiven,jigsaw,jmeter,joe,joomla
jspm,julia,jupyternotebooks,justcode,kate
kdevelop4,kdiff3,keil,kentico,kicad
kirby2,kobalt,kohana,komodoedit,konyvisualizer
kotlin,labview,labviewnxg,lamp,laravel
latex,lazarus,leiningen,lemonstand,less
liberosoc,librarian-chef,libreoffice,lilypond,linux
lithium,logtalk,lsspice,ltspice,lua
lyx,m2e,macos,magento,magento1
magento2,magic-xpa,matlab,maven,mavensmate
mdbook,mean,mercurial,mercury,metals
metaprogrammingsystem,meteor,meteorjs,microsoftoffice,mikroc
moban,modelsim,modx,momentics,monodevelop
mplabx,mule,nanoc,nativescript,ncrunch
nesc,netbeans,nette,nikola,nim
ninja,node,nodechakratimetraveldebug,notepadpp,nuxt
nuxtjs,nwjs,objective-c,ocaml,octave
octobercms,opa,opencart,opencv,openfoam
openframeworks,openframeworks+visualstudio,oracleforms,orcad,osx
otto,oxideshop,oxygenxmleditor,packer,particle
patch,pawn,perl,perl6,ph7cms
phalcon,phoenix,phpcodesniffer,phpstorm,phpstorm+all
phpstorm+iml,pico8,pimcore,pimcore4,pimcore5
pinegrow,platformio,playframework,plone,polymer
powershell,premake-gmake,prepros,prestashop,processing
progressabl,psoccreator,puppet,puppet-librarian,purebasic
purescript,putty,pvs,pycharm,pycharm+all
pycharm+iml,pydev,python,qml,qooxdoo
qt,qtcreator,r,racket,rails
react,reactnative,reasonml,red,redcar
redis,rhodesrhomobile,rider,root,ros
ruby,rubymine,rubymine+all,rubymine+iml,rust
salesforce,salesforcedx,sas,sass,sbt
scala,scheme,scons,scrivener,sdcc
seamgen,senchatouch,serverless,shopware,silverstripe
sketchup,slickedit,smalltalk,snap,snapcraft
solidity,soliditytruffle,sonar,sonarqube,sourcepawn
spark,splunk,spreadsheet,ssh,standardml
stata,stdlib,stella,stellar,stylus
sublimetext,sugarcrm,svn,swift,swiftpackagemanager
swiftpm,symfony,symphonycms,synology,synopsysvcs
tags,tarmainstallmate,terraform,terragrunt,test
testcomplete,testinfra,tex,text,textmate
textpattern,theos-tweak,thinkphp,tortoisegit,tower
turbogears2,twincat,typings,typo3,typo3-composer
umbraco,unity,unrealengine,vaadin,vagrant
valgrind,vapor,venv,vertx,video
vim,virtualenv,virtuoso,visualstudio,visualstudiocode
vivado,vlab,vue,vuejs,vvvv
waf,wakanda,web,webmethods,webstorm
webstorm+all,webstorm+iml,werckercli,windows,wintersmith
wordpress,wyam,xamarinstudio,xcode,xcodeinjection
xilinx,xilinxise,xilinxvivado,xill,xojo
xtext,y86,yeoman,yii,yii2
zendframework,zephir,zig,zsh,zukencr8000
Enter fullscreen mode Exit fullscreen mode

So, let's consider this extensive list and build a command for JavaScript project in a public repository.

  • OS Files: linux,macos,windows
  • Editor files: jetbrains+all,visualstudiocode,sublimetext
  • Project languages: node,react,reactnative,vue,vuejs

EDIT(thank you James: Now, a word about local OS AND EDITOR files. According to Git Manual, these files are best placed in a local .gitignore_global file in the root of your home directory own your computer.
So on my Linux computer, considering the editors I use from time to time:

gi linux,visualstudiocode,jetbrains+all,sublimetext,vim >> ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode

After creating the global file, you need to configure git:

git config --global core.excludesfile ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode

Now, all repositories on your computer will ignore these files.

This final command will create a gitignore file in your current folder:

gi node,react,vue,vuejs >> ./.gitignore

Enter fullscreen mode Exit fullscreen mode

Who wants to remember all of that? Create an alias in zshrc:

alias ginode="gi node,react,vue,vuejs >> ./.gitignore"
Enter fullscreen mode Exit fullscreen mode

Now all you have to do is issue ginode in your project directory.

I hope you have found this helpful and feel free to start a conversation.

Cover Photo by Markus Spiske on Unsplash

Top comments (4)

Collapse
 
nylen profile image
James Nylen

The .gitignore file for a project should only contain files directly related to that project, not anything that might vary by OS or editor or IDE, because this just clutters things up.

These files should be placed in a global .gitignore file for your whole computer instead: help.github.com/en/github/using-gi...

Collapse
 
eclecticcoding profile image
Chuck

James, thank you for your response. You are correct, this approach I use does create a large .gitignore file. In a perfect scenario, all contributing users would maintain a local global .gitignore file to exclude their own OS and editors, but unfortunately this is not the case. I have contributed on many Open Source projects that were littered with superfluous files. The biggest culprits are editor temporary files. The fix for me is a more robust gitignore file.

You do raise an interesting point though. I will include a new section to the article which includes the global .gitignore and how to properly configure.

Again, thank you for your contributions.

Collapse
 
nylen profile image
James Nylen

I prefer keeping all code including the .gitignore file as clean as possible, even if it means more carefully reviewing each change for stray files (something that project maintainers should be doing anyway) and educating people about how to use a global .gitignore file.

Collapse
 
vlasales profile image
Vlastimil Pospichal • Edited

I put my own .gitignore into .git/info/exclude. It's good for project-specific excludes.

Or for global setting:

$ git config --global core.excludesfile ~/.gitexclude

example for .gitignore

vendor
temp/*
log/*

example for .git/info/exclude

private
prototypes
Makefile

example for ~/.gitexclude

.idea
.*swp