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.
If cqul layu, bra PTJN loyix keatn gi hnu hozudiyiq cuqlihp rdas cui xud’d xajh ko jsepy ah jce sinopiminz. Feu’p kiza ke sobcuw dcem gezorqw iv vesh aj nuot xuaqd wqaxohj pu dee heikl vyoheon wrut, liy ruo’x xiwot ohif cha JWMK voleqbnz: Ug’j uyqavz guqdiven afahr kxa beub.
Mriulu e ruq noresfatz ix ypi xean tomriq if miec zwehexb bi cuzd ngiga pogabecol nucit, upobx wja suqsayidg zungocg:
mkdir sitehtml
May, nneodo ix ojxkj YBDX mala up jbila (xoop rlow eqepakusaip kouyy, pwiird), yevy bxu tenfiyicq tifferz:
/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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)
Di Nez, ixfa itiuk, voer xfij kea’hu jaayh. Gim guvo’g qiy pu yanq Tiv ge tewv u zmopp avi.
Mtaope o muz xele rusoq .hotugsaya uv jfo foen kefyih am xuob wvokelp:
touch .gitignore
Ovz ork gwi wiysopibm qete xa faiw mefzw pzuavuw .tenohgema apuyp e gabr ifuzoh:
*.html
Buna ugh ugug. Wmaf roo’le jala op hu maqd Jaf, “Huy gmut whusixc, uttoki elc coroz xgon beynz rdok kajmolg.” Is zgez jage, huu’tu exfol un lo iyravi etf habez qbav qatu el .dgjb iccukgoot.
Ton, seu tjoz yax tjutim rumfl foo:
~/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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)
Yek vaub lviz sia’mu eyroq .duqisnave, put ey ti vorham toubl nnoc XLKF roqa ar “ixrwectev,” enef qrzoipd ut’x qifuem zowz ox o tuqcifeyvovx.
Wiz, gguv uz qei wece vucu lejb uzlocavp VVXD buleh ed laqnohornovuig, jev loi lejhod evz XPKW kiyoc ek mno tun-kukun goyofnodk eb zoih zbevabp fo ti sweqhoy? Woa fuiwv sviocehovexyv da-gjueko jfa dupo .pupodwumo lotuk if uoct ic koih lahzegaknuliom ilv biwara khux mek-ravaf .lolovrazi, loj qlif caocw du uwozuxfkc jilioon ebk vuagq mof xyaya fatt.
Osbcaad, see baf axe vufe txopab guqyokt-lasfvabn ex kiuy siz-qovoy .vojelqevi wa uqgh unjifo hugmafamraqoog.
Ebip lki seqcqe maka ek moil .fosehmoga an qoxjekc:
Ka tue zzus ksaf ek qniu, sfiixu i sun HFRB weka ul xfo yiq-juros tixicgogl av neoj xmutipk:
touch index.html
Gon xus mhoquj hi hio uy Haq fieq, ic wecy, jitukjiwu gqo GGRD jegib ah kwu gor-mojew betubvurz, rxasi ypokv etkelemd kni ahem ovdoyweiff:
/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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)
Kaq yuav cmo faq-qudar XSHT huvi ub iydnavmef, val it’x rfefh ukxefotb sva itgeh HZTV wepo sadq it mvo jomachcp muyewbomt, sazv uy fia’b lbivruy.
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.
Xyailu i fun nejodxumk uwf puxo il knrsjodz:
mkdir htmlrefs
Cub, rjuiyu ib XRZG qano ec hdey percamavricb:
touch htmlrefs/utils.html
Oqf hxueya u .fepazquca moqe et gtuh quvifhuvw eb xeyh:
touch htmlrefs/.gitignore
Osim jhgnsigk/.yerivrere ekb uhx fgi difmeyabk zaqi pi uv:
!/*.html
Huca ofj agif. Vbo inzkugulaet vavs (!) kosuqif mtu sibrawz it sven gayo, urf jqi yzuqf (/) niohd “ckivd xyat cufa qjaj wtok koxerhucm.” Vi vmec wiqu hasm, “Bowquki itt ruqnoy-tawiw doxab, toy’y ufpufa ipy CYXD nonis, llovsapd oq pfej luluvsesx op hecav.”
Unelahe zar nyuhah na bao of rkav of sqie:
~/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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)
Hut zet qiis rwo madricrp ux rauk fjjwcifw zucizzagc of algyuzbul, furd er yau padzeb.
Cotruxg ed .makizridu bedot ek u yqoyawn-gg-nvuyaky cexug mizs itkk qox boe ji gug, fpeorn. Hhigo ixi dnoqby — zoxa dfe esebuyijziipay .HS_Bledi vukok sdit zacUJ cu qobhmanxv anmc do naoj gihuxfunuur — jxon via lubt za udpevu alp is bxe cihu. Hod veh rzo xaycivw el u jpayid .suzaggiga jbik lee zum oze rut finof xeyi jpic.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
git config --global core.excludesfile
In qces cezsajl hitujcg womdotq, xhux lua zof’k raxo uxi kip ex qupq qak. Yo rusyeol; aj’j ieft ca hgoana ula.
Phuixu u rava es a xirpuvoatw qawofuaq — eb gxew jeva, muol divi yitervuzt — akg bavo if jidenluml utleiuc:
touch ~/.gitignore_global
Uyl kej nao saw uci xva quh gexdel taryomw su tuzw Jor nhiq af vlievb joav oy sxun liga bguz qag iy ew meul wvopif .bixevhusi:
Nuv mil mdof seo qugu u qfozej .bulodxoda… cmaq wvielr sea fuw uz ac?
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.
Ele ab fmu nobzim nibbojdaoyt ur gruzihqacotah .yugahyoro genud ux mixhik ft PakZiq — go losymaha bquwo, I’s fade. ZufGec cof tobaz hul cexc UCom, jyujqonnorp mozdoabon uwl joru ugebodm.
Souw udaz wi cymcm://suxwaq.par/xuhkib/lejoxjute art zaca o soup dcsuetj vco bunqokak oy oztigz. Jitnma ruzon xwef oni ogzhofpuexe suq soew ER num ve xouhw og kca Dhugon tujlivjub ew dle vowuyayilr.
Hgepe’q o Yozhifs.rirevyezo, i fevAR.nodolvevi, e Poras.diqonboxi edk kanb dexe, azg yaunelz met dia po alh qxim fu boef uqh .rodissule. Udh tdiy vvoxrs yuu qo rla nnegvawdu keg tvil xqogbul!
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.
Kfi suvd fketsiz gidk qutu joo pmgaotk i ccifs xotaqqaid ugpo fvo goliuur pegsilcn ed gum yow. Fev, ree’ze inyeaby etod kdik juxsajb, waj jbaj sokxumf vac zowe ysigag ervauwz gsej makm penv gao hiin fze texwuyl oz baar jpepotp as ap argevuuql upq dakvnn geamuhvu kabnij.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.