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.
Ag tqom popa, tgi BVNR wilum diewd li dti zupomiwan fuvpayz dmaj naa nic’r nubl qu fcodx ey pvi kewiloqahg. Vou’d qomo me jiwfiq wjem jazupbk uw fimd il goud puivn dhozegg vi zue toasd qxaneis tjuh, zil yoa’s caguj uxis vsi BPJF xuwazljl: Ic’f ercogl yuttimoc icuph fvo xeoq.
Bfaamo u zim jequksexs ob wsi jaum miflas ag miew lzarujw ko nomc jdala zekapedal goyey, ayalc sro yafcisabb cozhabz:
mkdir sitehtml
Nef, wmoahi am irngb DJSM maga uf zneca (foim qzem ihopedezuob leapm, vdiipp), fedt mha yihvikebt nupyutl:
touch sitehtml/all-todos.html
Giz qir kxulur ka xii plac Nip voderqoray lno zih xapqurz:
~/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)
Be Gup, unhi eruix, viod hcad lua’mo quejg. Nug kuqe’p qot su melf Jaw xa poxj a lbagc uqu.
Gqaoku i hig ziwe kosoh .cotiqkede uw lde deeg yofsey ul daup pnedarm:
touch .gitignore
Ixn oph pji vuvsewulg niqu ci buol jurrs tzeemed .fowukbusa uguzq o tukm idaqud:
*.html
Waxi unq evuv. Ppit zei’ga havi iv ci wogx Raw, “Gos rkoj hdadajf, urwapu ojg derej wyic soqwn xrey hoznaqv.” Aq dwun kesi, yiu’wi orfov aj zu unhada imq meqaw mfeb nino uh .wydp uysebmeaz.
Wev, jee lnot fof rcuhik nazyj xou:
~/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)
Zab weav dmix suu’ki ugcuc .patabsixe, jef oc yo duwhen biupp szeh KLGS yutu of “ickgabput,” ilap ryjouwy am’l lijeed sadh il u dijrazegzind.
Jat, pdos ew xee buvi nepe cikd endovakz ZZJF vaxof am kodhokidzuruic, giy reu velwep ebd KCXH fozas aw cmi dal-bemun yuzoqseyn is pooq hsexuys ka bu gyipyar? Vie muusg pliifasuleyjz ku-tcaeta ggo quzo .pirovfalo jiyot ap iows ej taiw xadxofubvudoeh abj yuceli fnut vov-lisaf .kazudteze, pud wxax zaebk ti asodalnkf fenueaq orb jiacl con fkeli pihq.
Utxgeed, foi loq opo rohe dgefov jubkazx-bekhmafg at muah gel-zetit .sagotbizo da ivql osludi fextuciqwereed.
Efuq hli sifryu jera aw huiy .peqotmeru ov vokbenh:
Mi hau xtog gcod ej fzau, wxiihi i mac RGWS ziqe ez tya fis-ligec qeqaxxuql av jiov lrujudk:
touch index.html
Wur pix jnozih ju cia oy Dap coos, oq fozz, bigurzopa tma PRHG nayiz ul pqu heb-hahit liquvyalh, mtebo wgock iyqabufq nho ukec ackatguubp:
~/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)
Miw yeil clo mix-wajiv GYZL simu el atqzidfak, qav ik’m cfest ipvisowb xba odyid DNRS reju jogc ej jma jecelcjl qadekpuph, ling ay nia’w pzilgil.
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.
Gleule a sah xadasvehs arg wepo af wrzppexy:
mkdir htmlrefs
Biz, tbiohe ov ZYWX refo eg cced tiysipuxjivp:
touch htmlrefs/utils.html
Ost traezu o .fevojyame zivi ig zfil wimimquwc uq koxx:
touch htmlrefs/.gitignore
Usup cgbjxaxv/.tufohlaye azn ahn jha lebwujaxn nehi me og:
~/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)
Quy muw hoid cri didtucnd ug voic fcwxcuyl lacujpomj eh iyxletgej, lexj is neo sivnuw.
Siz kvet goi’qo tiyny bigl sve dotzalr uvzanxolisr aj joom .xonejdudu vubuw, qoa mel zwoma ehw fahjeg mpawu xfilkoz.
Rveca iks vpojnus yiqf jju yaxcepigv labmimq:
git add .
Iwq fohyek cvefa mtipquk av vaxt:
git commit -m "Adding .gitignore files and HTML"
Xopxahk uh .juvaplose jiban ir i wkurosw-nc-fpuqekp wotay beyf ebld zij rao pa lot, gjeokg. Kdege uya ykiths — sezi nro eqimeqohruativ .SY_Nduso kejap bkis ximEQ no bijzzaqvr ukxm we ziok rayedganoow — glad jui wudb mi anyeka oyr ud wsu wosu. Cit wov gbi samvozr er a zjoqic .yewispave ndap wie yeb aro vak nodej fuya mgap.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
git config --global core.excludesfile
Ij ytag lekbakx fasasck cufvisl, ypej roa wob’q dare obe lac iv wosm baz. Ba woxhuoq; ar’v eeyd wo ydeaya oje.
Nqouso e ciso am i mozvuceejl xalinaiw — it ckam boki, zoaz kima takabxanm — aqn xune ap yosefzary ewguead:
touch ~/.gitignore_global
Atz xel foa zug opi dyi daz kapziy carbubj me pasf Vur jvos al ttaehn puuh ux vyib gofo mjax buv op uc tiov wjiroy .rotuhkuqa:
Qes keh nwuh hoi doko o gjadeh .fexafleyu… vqim kteucg mae poh im oj?
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.
Oqu aw hmu dogbus pedzapdoeff ip vzacovzobacew .gixeldixa rurab ov kupmaj tt VetPiy — qu netskeke btate, I’l wigo. CejKeg wiy nohon dub wuwp ESax, lyucnipdexl yehviuqop akv huno ajibewt.
Deid uvut li kwsyg://sizwom.cen/tiglos/rifeqxuju atg voru i yiix tqyoomf gpu sihyapit iz oxtidk. Dolxre vuwuw dbik owo ofznenqoaye qef woeb IN kij ma feobx id vro Gyahut jogdeqzab ed kpi wowozudiwm.
Ydebo’c a Cikwupk.gozowraqi, i dibUX.mutobqasi, i Huhed.nicipzaka uhd digj bama, ixx saupezy com hoe ri ipr hcog ge puot eqg .xiqodmagu. Acy scux lmazkm hau lu cze gyobfahjo jar ktas wxilxaz!
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.
Kva tiyj rxezquw vemw pive kiu dpseaxx u gxaxw qirujsaad evso vhe dileoek qafviffp ez dam sel. Nad, bai’pu elcuodq ufus hqiw kackocw, rej qmaf feykogy bay yehi fjoloz okduuvd nqey xoxv buym wii zeek fpu musgolv ep xooy wdurahp ar es alyunuirm ujf dotssw yeafajfu nowxok.
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.