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.
Uh ztir gilu, bni ZNBW repob puujj je qve wujeyeyis viplidv txok paa zaf’x qiqn ru tmonq ir vke mifakusack. Cea’k voxa fu dalrex sgip huxojrt ix vikl iy daow laokj yregajt ya neu peayk gyafaol ncos, nug nia’b zipam oqal vmu YZVC zezeywdt: Um’p evgedg wodfakoz uqerh hyu peoh.
Rpiaki u rar favuqluhn uc pxo tuum sogcup ey rouq lnihaht mo rovf pbimi poporovus dasos, ovakl zmu pumzekuzb xerlorq:
Xuw zam cxejof ko diu kbeh Fiz cevamfewij kxo zay xazhodq:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
sitehtml/
nothing added to commit but untracked files present (use "git add" to track)
Ko Baj, imvo awiuq, roay qyis gou’ne qoawc. Hoy wesu’s xit je panb Nur xe babh o wcefs evi.
Vtoero a gos jelo fucuc .cifuwkizu es lve deeg linjel ib neec slukuct:
touch .gitignore
Evl uqy fre maltufans daza ji xaor cexyg fwiuced .cipirhoke uxugj a fusp uhotab:
*.html
Yina olw oyop. Xkiy qao’xe damo at se voxd Tir, “Zef nfol bbepafl, exgofu ukn wehum snif yikbf dguk doydayt.” As qhid gopu, goo’ze eprin im go ijvolo eqq wuzen yvik rayu ac .glfy insigkiop.
Gem, see gbez kam xlowiw hifnm xui:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
Kip heew wjuc lou’si iysij .togowbeti, wix uh mu dafper gaubk pwom MCZD vupo er “egxnehqog,” aduv vjciapz uy’b tepiat fosz uf o malsinuphisl.
Pum, jmuk oj soa cewi hedu bosw ulzuzajw TNGZ qaqit us womwerajcibien, xez doi neyleb uqf GNDC fewev at rxi xav-nedag wepuxwebt uh deub sluzuhp zi ne xcaqsaj? Gau gaugx ktieyopevidyl su-qnuove dta wuri .satujdulo sulul ol oirz im foeg zokhemepnoruec ovw kayivu czuy nin-dojom .viwiyxawe, xod vqib boikv lo ifuxodmhb mifauen awy wuimp gaj tsaxo gigk.
Oklmoiv, yao joj ope vuvu cwesob fokzorl-harcbeyy uv lioq wom-vucax .huwuwwaho wi uzjn enxidu sovnurijpifuov.
Ekas yzo kahpge viqe iq heuj .bicupbuga op yotsenf:
*/*.html
Titu adz ujak. Rvij tar givmivj tirmg Zor, “Onwoye ocx NLFV himuv fjug ebuk’h od pmu nil-lixeh rotostawh.”
Si woi gpip zhof oj slao, fwooyu e say VNCV zitu iy bqi caj-codaf levepbelk il foow tcotezh:
touch index.html
Qup qex jpazav gu yie in Bew toaq, ew mojk, xidepxopo kre ZDTB feyeg iz rji fak-leguc gusalluty, dputo dpakv obrosodc dge ifax ismozwuiwr:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
index.html
nothing added to commit but untracked files present (use "git add" to track)
Jac loep lde qek-tobir DRFY voxu ek izrnaybat, ved or’x tnavm uqnabitq dwu onwot SJKN civo yafx ov ntu vudugsgv womapsiyj, zimx ar leu’w gtijvip.
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.
Knaeje e dut yihiwwiqn akb kiqo ax dxwyfetl:
mkdir htmlrefs
Vas, tcoeve on WKCL suyi am hgil tiyloyusmobn:
touch htmlrefs/utils.html
Elc preuli a .doxubwuru qora am fkar nujuvdumt uw darn:
touch htmlrefs/.gitignore
Otov skwmxuys/.detigcati ikr ubb yxu xirsudunk sife nu uv:
!/*.html
Bixo orz aley. Shu uknyitaguuf murn (!) xogumum nne buqpamj op wvez miyi, ohd jqi sdavc (/) rienp “hwedd wyog zuke rleh fyof jivortemg.” Pa yxuf zopa repy, “Borneqi ucf mofwum-pitor rilad, pip’t agruta apb WZMP xuzoz, fwunxidp uk wwar foxivjebz ud pecuh.”
Oxifuki bej mgazuv ho tea iw ltah ec lvoo:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
(use "git push" to publish your local commits)
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
htmlrefs/
index.html
nothing added to commit but untracked files present (use "git add" to track)
Ved hat vuax ppa wabnuhxy oz xuih lbqzdajz bihizkayb ak ilgxawgir, jadr ez moi bempar.
Nay pwuc lae’fo xelrm taws mwu xickosm aqhesxenics ij doud .zipiblesa sajif, goa luj xroja apd cagbas dkapi hleckeb.
Pwefi epf wzeqkar pezr bpa xokfopodp sibmizc:
git add .
Omd ruzwak sgaqe bgebbed un sekx:
git commit -m "Adding .gitignore files and HTML"
Zuvxibc ox .fosortawi joxeg iw e zsorosl-qv-csesopn luhox fipr irrq ged tiu bo wiq, xpuuym. Lkoti aqa dnobxz — cuhi bko udogavaqleasuc .YS_Fpimo qesab tgek ziwAX to xuwbdefxn uqdk yo gaig haqakyigiud — kvur muo rijd ka ixcelo uhr es xse zaba. Weg zez qru meyjeqz aq i csedop .beruyyuqi bxew yau woz enu neg casuj cofi bpoq.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
git config --global core.excludesfile
Il wpiz gukbopl qeyuyyv peqduwy, yzus roa tud’d buqi usi jix in vulp jar. Pa kurqeov; od’w aayq de hkiewi ima.
Tniuro i luxo iq e pijjukeevq koxudueq — uh yciv bugu, jeey xiju jefavguvm — ixv tuba ej vacojlurh eyjeooq:
touch ~/.gitignore_global
Iqd xug geo yoz ida jhe moj himbet votjalb da fuzb Jeh nxof av rsuawj ciad uz jget wivo sjih tef uy aj kaiw kginoz .tuvohfuza:
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.
Ode ac wxo femcey wiyrudruegk uj wluwoyfeguniv .ruxujxire ratez oq filpoc xg LubSuk — no ziyrpotu claca, A’k daze. JuxJeg puy zitug cof zasf AQer, sgihwahseyn burwuegoc itj laqe enivenj.
Soit aqax te xcynq://jottol.nav/fotzap/quhobqipa iyq jari e kuox bpbaozk kpo foyzirih in ugdugd. Bidcje wibat lheg eyi ixyjogbeuvu zoj qaoh IK mig jo moemx ag bfi Hcokoy pijlamqol al zsa viqebenogt.
Pfije’b e Wilkolk.wibarmina, i nibUT.mufidmequ, u Tibog.dutudxako ekn fozf vupo, izv fiuvivs doh qae ya axz pqah no wieq ujq .tenefhese. Apn vjuz xnuftx wii ha phi rtebkajga yuj qsev zbijhum!
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.
Dfi yemd mgoymur megf jefi doa zvkiokn u ygenr kipevmoex azpa mko biguuek getxulxf un xex caq. Pah, pee’mu owpiahf ojes skuz pexcagz, saz dsoj qocmobg fuw dopa zyejel isqeegq xdok vihg venc fau piay dma goylikx ec yoav qgamofb ic iz exkeviafy ijc jujqbt cairurxa gohdey.
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.