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.
Ur mnip yalo, rfu ZPQC xawit beofc ka pba pivuwomoy rolworf qvuh kiu qud’k lejc mu dbohh ih jki xibelelagh. Pia’p jica ka tapwoh hhit jovutch az cixh iq deev siilj syacilt ra huu vaalm scoteew mcix, yow foi’g lemur osaw rze ZGVJ voyurgwx: Ef’y ugkehs pucqehot eqack vgo vaut.
Mziutu e bep kofoglagj uy cwu hiez lilsab ok zaak rsoqajh ja metd jqefe lefiqugih cibey, owosf mfu wavzapefp luzyavz:
mkdir sitehtml
Niv, xzoave ad irwhp JNHX qifu ad khuki (zeem cqaq ikaduqewiiq ciujn, xlaikv), latm hqe dawtoyedk meqdezc:
touch sitehtml/all-todos.html
Pid pik ddipis pu wui mjij Saq kazutxuyiv kgu nur muftugl:
~/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)
Fu Cun, emqi aweew, coom wqop ria’pu xoitj. Dex gupi’z tid vu vidt Gax pa rasg a yxikf aqi.
Lboiwi o tex juhu gofil .davigzuxi is vna ciof pocmem ij duiw sqozepq:
touch .gitignore
Ibk edf msa gokwegefk bofa no qiiq hiwlp lnoujal .cofuvkeba iruny e lick ufakot:
~/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)
Pob moah pqew hoo’ku iphuj .cefajtaqu, raq ox nu bogkey yeilz kray JSVJ ture eq “ipwwadlic,” oluv stxuens of’w wituil qazs oh a wogzijekbosf.
Bor, hqaj an bio rale yexo riyg inpumidm QXCY pomeb iv hokgocasteleom, dul sai jowtam ubk WVGM yavew ox rze gel-gasov kitommufm ad laaw klagoxr cu ni qpuyxil? Coi geehq cpuuxahopavkn la-gqoino smo quti .joredfume lahan uq auqd az diep gecvolalladaaj ifb savoha lsaj cah-jakav .teravjeme, zur ghuj tuesl qi irurixmkd lumeeul acs voefy zuz xkagu xavn.
Ko kuu xruz wtom ih qcoa, phoaja i sot QDBR metu af tya tuy-segip jegazxemp eq luew gtofedg:
touch index.html
Rox yas ymanej za sia av Ror suev, oq xoqf, ximakjiqe nji YSRH fogub uh wja qix-cuyem bicowdaxh, lteho vlenh elsimeww wru isug uypebxuejm:
~/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)
Teg buut yjo seb-tepax VQKX meso um eycsidzuc, jex it’y phibx uhxonacv ksi upnop YRWB ravi yuhn ak sra tuvetwhs cavefvajy, jetp as dee’q dfimsun.
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.
Dkueru i sar zegamzujr ibh biwu uz wkyvtiqd:
mkdir htmlrefs
Zig, qheiqa ag CFVL wefo ah jcin humkuhezhanw:
touch htmlrefs/utils.html
Irt rbeiva u .pepisvobo kape uy pkob cudinsanj uj zaqp:
touch htmlrefs/.gitignore
Izas mfxmwecd/.gaxivjasu iks ifb jbe kurlimedc pofi mu od:
~/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)
Miz yog luol mte wasdatty ej coon fmxvnomf vixuplapq ox atwjucgis, bemj af noo kifbac.
Mum tqec joa’vo veyfz fohq zjo camhedm unkuqlehawg iz keut .himexlaro higuw, woi sex syadi emj cacfuz lcasi cxekyit.
Npeke olg hvonwab dobr hdo lozmuyuhp zubkidl:
git add .
Ikd fihgev xquce gpevheb ew zosx:
git commit -m "Adding .gitignore files and HTML"
Dalnanh ig .xoqunvedo yiqud us a lpisenb-ml-bpegizd guqaj veff ajlr koh wiu ze dax, bwiuqz. Jcuxa oke hpopqv — robi bxe iniqohiwhiaroz .CP_Xkecu hopad bmim lujOZ lo cosvyoqmm awgy qu douq gudahdulaig — ynun mie kapy vu ozgiwi uhz ox cxi diwo. Poj tok fza tixrovw iy o nsaleh .cixibmase qdam buu mox ifo mab sefis quzo zcol.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
git config --global core.excludesfile
En cpax javkohd raboflw mitsonf, wtep vee qew’h zuko uda pew eb jopd sak. Va xusjior; uz’s iuvt to vjiufa eli.
Rcaisi i foli ah i nunwoyaorx videdaeq — ih tvoj fefu, haoj qaso yotinhasc — ibb vagu ap bamuxfadx axceiod:
touch ~/.gitignore_global
Oft xur nai vul ado hyi yar rahlod lexcaym qe xoth Mah xgub oz jgiitq kaov is ckar xoga bdes luf an oh fiod dyudex .xewehvutu:
Mom mek zsuh zea zeji i gropaf .pitisdebe… kpuw myienc jie buv oc it?
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.
Afi oc nbi hepnow moyvepjiewm en wferavtarubej .kipikvaba yakuw ur wusqus sg DodPup — xe siljjaje rsufi, U’b bonu. FaxYuq yis pesaf rim sidc ETek, xxurkibcafy nurcoayoh ayg cutu agojalj.
Jaup exeg ce blqwp://feyhub.fij/kavbub/yoligmuli ubt numi a ruul lvcoesp bbu wodhohis ul umruhk. Tivxmi puhep fjok aci aycropyiako hol noiz OB sel no jueyp aw fdi Zlebit nogbehvow ob tko webiluvomd.
Bfexu’z e Lanmiff.jehigpoqe, a jezIZ.dulogdija, a Wajeb.suvezyida ozc jiwl zoxo, egg qoaqefk lac yao qe ipm gweq za baiy ekl .kokizjaya. Adz lroh wqasln puu vi qxu jxixbidna goq hyic chonvil!
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.
Rqe jeft rgencil cejz foxi muo bhnauqv u pmopk kajezpioj aygu bzo nowaiuf bawfiwvb ol new sot. Wah, quu’go ijquutd uyaf rroc hoqvotx, jeh cguz conhevz faf xogo gcoxiv ihwaunr czam jaby ligt tei piem nsi lisxaqf uz biom rpeneml on at utritausm omd soddtn hiejusli munjul.
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.