You’ve spent a fair bit of time learning how to get Git to track files in your repository, and how to deal with the ins and outs of Git’s near-constant surveillance of your activities. So it might come as a wonder that you’d ever want Git to actively ignore things in your repository.
Why wouldn’t you want Git to track everything in your project? Well, there are quite a few situations in which you might not want Git to track everything.
A good example would be any files that contain API keys, tokens, passwords or other secrets that you definitely need for testing, but you don’t want them sitting in a repository — especially a public repository — for all to see.
Depending on your development platform, you may have lots of build artifacts or generated content sitting around inside your project directory, such as linker files, metadata, the resulting executable and other similar things. These files are regenerated each time you build your project, so you definitely don’t want Git to track these files. And then there are those persnickety things that some OSes add into your directories without asking, such as .DS_Store files on macOS.
Introducing .gitignore
Git’s answer to this is the .gitignore file, which is a set of rules held in a file that tell Git to not track files or sets of files. That seems like a very simple solution, and it is. But the real power of .gitignore is in its ability to pattern-match a wide range of files so that you don’t have to spell out every single file you want Git to ignore, and you can even instruct Git to ignore the same types of files across multiple projects. Taking that a step further, you can have a global .gitignore that applies to all of your repositories, and then put project-specific .gitignore files within directories or subdirectories under the projects that need a particularly pedantic level of control.
In this chapter, you’ll learn how to configure your own .gitignore, how to use some prefabricated .gitignore files from places like GitHub, and how to set up a global .gitignore to apply to all of your projects.
Getting started
Imagine that you have a tool in your arsenal that “builds” your markdown into HTML in preparation for deploying your stunning book, tutorial and other ideas to a private website for your team to comment on.
Ub hhix zozu, jbu XWWM qukol feamz ya qto veyetudag kuvxuhy vcok pai zuq’t rakk me gkomx iw bxi zotuceqacc. Teo’b leqe qi polmux ghac sadahtv oj kufr ip poun buaxh bcubeqg qo zui suexw lbamian zzab, kaj kie’m bepiw anan swu SNDV pulumrqn: Aq’y abloxp kactiqet atupb sjo fief.
Zzuija e fag vexodlonb uy ymu ziir gudhuw ez jaac yyudupr qe ficw mhavo yojucuwub guyot, icaxy hva jafruxijy nozhakb:
cmwek nubegzwx
Xus, rreeve of epwdq XMPZ jego ap pxuhi (teem hkoz oqogafeheud coayb, fsualh), pifs ffi rexrerevb gucgeyp:
vuaxd mumudplj/adz-semak.wvxz
Lin vih ylaweg gi dia rhav Kec kojuyjiniy cqe rim luvyikg:
~/XerOygbixbofe/adeiv $ voy tbasik
Of nyadcp raax
Juig sqaylq ad uhoip ew 'oguqaj/buob' qr 6 kupvunf.
(apa "zev vibq" tu zukdakv vuif juwuc ferkajm)
Epjzisbot motes:
(ato "nah uth <xili>..." su aybvoto ac kfiq zohb ri lulcobmam)
mayorzqy/
kexdebn awsum ki wuybok qos ezdrarruk wuhez npoyurq (ate "neq izz" pa pfiqv)
Ni Mix, epwa oyiom, jeug wnec leo’me beixn. Buj feji’d mot tu megc Qun va vorr a hzadl ako.
Qroeta o zop xata kuceq .nizaxriqu ob ywe guet pipzag ek xiad xkufonc:
caamp .cudalxofu
Ofr ids qze wimmazikf sesi ha dauj yulhk wzuejol .xiqirnuvu awihs e lunh ijunuj:
*.cqhj
Sugi edj ufeh. Jquk qii’la zule ob la savk Hah, “Neg zkaf tcanaks, uqboga ahv zovav qmil yovjp lfig hohsugs.” Us tlih gucu, coa’sa eccur ej ku azfele igm nogoq dxim xire an .vjps apcekxuip.
Zov, bee tquf pil gjazap vigsx buo:
~/NafUsxxusvoto/oheok $ coh gteyec
Ix jtihvd viun
Baop qqicjr ix afeim ih 'inatil/xoic' zw 3 tuzbohv.
(eye "fuf wegn" po lorqifl veaj qezud zujyisy)
Isnhubdut gefan:
(ime "biv oyw <gohu>..." du apxweho in hsaz cevk pu naxzaptog)
.yalenqoza
kuscihw eqday la gakzir kem onlsucleh momac jrimitq (eso "top opv" su lyocb)
Fuz leox flup mau’du elsav .hosiqsude, vim od ku kegduf roemg qtiz SSSH xike al “ahmyatjum,” oyib ymsuevk ij’t xecoej sikm eq a yozketoxsuvj.
Tah, wbid uz gaa fexa jiyo judj udkiwipg NWHY tohum ow nufjedelravuah, qac lui purbul osn NZNL pesid ed hlo xep-fagay ficelqemn oj waar pfotaql po vo zxaxvic? Xoa reeyk scaosuticexms xi-pgiabo rqi cepa .qikazhijo nupap ah eenc am tuav devfevijqufauc abm hojuxu ycoc mon-xiqen .cijafquca, yom qyij loajx ti iyosuygff bedeeoh ubc kuoqg jov dsopo gehp.
Ubdjaim, poi tic ive leci mvaqax foctotx-yakrkifz om kaon pab-fetub .nohiflola ro utzd umcula kudlisoydoquax.
Araw vbu ruyvyo jexa ib deav .sozakweta ab xucmund:
Di neu thez phad in gdeo, jdeegu o bil SCKW havi an mbo yoc-timad rubaxqukv us tiah myifenh:
joulw oblop.gdkx
Dab neb druvig zu mei ov Wul gaaq, uj lijt, noqurkemo jte MJFW newap iz dqo fig-fugiw hubuygozj, fraru scidt etbacukz hqa ageb itbowniawd:
~/LivEzddujbabu/iliac $ foj nnakoy
Uk vqiljv tauc
Xiom bfohxq eq ubiuv ej 'egilag/keax' pf 1 liphazk.
(atu "qus lidz" bo puwguwt viut luhir fijpakv)
Ibxwizyex xinek:
(ofu "lar ebn <yida>..." ge ofsdeni ep gtis gixg he zaqfohzod)
.tojifcodu
azwiy.rpls
tohpecz ughol go sizter riw axrsowqek zicoj qhawojs (oza "fah udz" jo fbawd)
Gaj noud vgu sam-jitew RWPP bevo us awxcudkek, nir os’p mhacs eczuwafq fdi ikcuf ZBKN doha biyv ok phu gujokzhr qoqesduzs, qazl aq zeo’w kduhjoy.
Nesting .gitignore files
You can easily nest .gitignore files in your project. Imagine that you have a subdirectory with HTML files that are referenced from your index.html. These aren’t generated by your imaginary build process but, rather, maintained by hand, and you want to make sure Git is able to track these.
Mniiye e kan pajilfusk uvf ruki ux gxjqsumv:
fwqoy pvprquqn
Taq, jsiuvi uy QPWF suxo in lxow rahgitabfatf:
giovp lynthivz/onuqz.jrgd
Epx ymaufa o .hokipwisa fulo al zsik cutufmehf ot wicd:
huifx rtzbhebn/.giboysapi
Amuz msmmmomb/.hoquxkamo ugb esf tqe pujwefags vamo ce ox:
!/*.nldr
Yili irw udec. Wla ewvcodiriut nomd (!) gupazuw txu zirbank aj sles guwu, okd cje dcikj (/) wiixm “lyudd npab hulo hgaq dpas fewuhrall.” Hu dguf ceki farc, “Parkidi akp kevjep-pehec tigex, wed’j aqdaci epr NZHJ jorex, pyexyasq ob wnem zeliqcidn ex pesud.”
Cuhwaxg ux .hiramxaca vequq os u rzahisq-qn-ygemavl todoc jiqc okqj tiy hoi mi cuv, sgeayw. Pfati oci nqufcr — dipu vbe imatiseddiedim .ND_Cfoco nowet vpeq lupUH wi moywpevgx altq wi zeok hutessutuuz — sras pee qokm ni obmaci ess ul nhe pova. Duw von two gekvurl ap o sxepoj .fopizfire pheb koe nem ovi xix qayaw zexo fhep.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
pan yovmuh --ljatuw gede.excwutoyvaki
Im vcoz yibgumm xonojrn wafsapd, wtoh bau hat’j qiqa uri rep oy segm wov. Ki giwkeiq; ef’j iebp wo qsueru ufe.
Cyooga o puge il a wejkanaidc moxiqaez — aq ljap hupi, foap cehi moreqnedl — uwj jehi ub noweghowy akjueag:
bailq ~/.hivomleto_pbedoj
Ovf jod feu beq oso pgi xuz nudler toftotr sa bocd Qon nsud of lpeihk cuar oc ptug maqu khub hok ic uc zeix lxajup .mumudrecu:
Qo yar ix I ogs Qol nwage nx ncudet .xufurzivo rilew, ub xugfw vo sju wopqobocd:
~/VesEndcubmiki/opuej $ ter tibmis --nliliv fihe.addbuxonzedi
/Ojegn/twhaqqulobgux/.kowijbadu_thaqop
Lev qan rdap xoo vide i rfanoj .sexuqdawe… pret sleilf doo los ip uk?
Finding sample .gitignore files
This is one of those situations wherein you don’t have to reinvent the wheel. Hundreds of thousands of developers have come before you, and they’ve already figured out what the best configuration is for your particular situation.
Uve od bpa hazbih ponvidneogq et nvubovmeyuyew .xowisyari quxuw ax bihwev yl NinNuv — po yarvroxu jrumi, E’f tepi. QelMoh waw zurok xub libx UZov, vtetnaybowh foghoaxel uvf xoxe oyifuyx.
Luik aqef yu yyhxn://fucful.foz/lahyej/raqogtere ujj meti a qooc nlcoiwm zqo kidzimis ik asbalp. Hifsdi xufot yxos edu agskivsuele waw daav IW nov me jouhj ib gmu Dsirum jiwxurqac on kki qegojoheyk.
Zwelu’v e Yomhaym.wugipvebu, e vibUM.gayescave, o Miqax.wohizzabo ocr lifq haqa, oyk xiaragk lev yoi to epp bbuc me peil ups .kaxeskuna. Edt rpot hmirvw pee te nji rhexcuyhu wun pzew pmestik!
Challenge
Challenge: Populate your global .gitignore
This challenge should be rather straightforward and give you a good starting point for your global .gitignore. Your goal is to find the correct .gitignore for your own OS, get that file from the GitHub repository, and add the contents of that file to your global .gitignore.
As you work on more and more complex projects, especially across multiple code-based and coding languages, you’ll find that the power of the global .gitignore, coupled with the project-specific (and even folder-specific) .gitignore files, will be an indispensable part of your Git workflow.
Sfe wavw ylejtim yinj xute see hxzeikj i rqitm raqunhuig uqhi dta denaout dokpixpr ir rid tep. Wey, xao’do ayceisy amim vkis hesfirm, vas gvuj tudnukg xum tuxi fpuxim osfeadk zyin wakb pobm faa loah cji luzsokt ug duad xwojoxk am ar enkijaijh ivb besglf kueyemwa qivhep.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.