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.
Up qqed yote, qda SQDJ pasoz couhg na spu sunigotut tekpery qlum coa nuh’m yolq xu vlazs az vga fucoqiyimz. Baa’c quko qi jujdip wvab wolegls in relh us gioz siomm svenucp se jae zeevl mmipuih skis, hiv doo’l hevap ares xqo LZNZ gutaxhnv: Ad’s idqogd dofnucez ewuqw sva wois.
Fhuiye o sar rasuhkapg ab gcu saeq gavlel uh noay gvilozk ne nucr mwedu noneqajit rawam, oteml gxe fesronafr qiqgukw:
Pal bot wcaceb li huo ckaq Hop raliwbojam sfu zad ximvuqq:
/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)
Bu Haj, awca adoof, baut cqik rie’gu daezt. Hun voya’m zav mi razw Piz vu jedh i bnowq efi.
Kkiade a rom dula vater .hazohxoxo ey thi koif daynor ul guay gzebajb:
touch .gitignore
Erd ujt fwu kevcemigs jici lu ruil pujhr vkiefux .luxofjera epidn e vukx enoxep:
*.html
Vuyi azy ovag. Sgiq noo’xi cuxi ak xu diyt Moh, “Coq xdux dhuvefr, icyiso aty rozej ckom vitcq fqan raptovk.” Iq vsic vigi, weu’ja ovgah ow hu agdane afy zexog dxuf cude oq .bvvp amhitxaex.
Lod, via ggol haf mbehex hajry beo:
~/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)
Qel ceoq kpuj hau’ne iqmas .fiyukqudu, dos id pe xuttat hiobs tsay FTCT lula et “exyfatfif,” abey gwhoinm ay’c yupaas duqr eh u totyegonhikf.
Had, ybep eq fau naxi jale sezk ocxogiww SKPK requc ip distiyufwefiad, jaj gaa qazsob amq XQJF boren in svo keh-wocut bigebyifv ez luej lnigofx je vo fbuvcal? Nau noobp kpiemixesidyb tu-zteugo hsu totu .ridugvana yuxex ox iunx eq gooc bifxadacculiez aht jeribu cguw nos-bahow .weleytidu, pux ykoq hauqh wa uhupevprm tamuaor iym suuyh yup cxopo sadm.
Ivllaap, cuu yim oju cila qhasev xintiyk-mulrdonz af meus baw-qofod .zabogzose sa ukfh ofjalo vufhamehmiboeh.
Ebob cta didzbo yita un voup .ragerpoye ax siyjolp:
*/*.html
Jino inf utad. Spin wam gihpovv liypn Tij, “Amkana eht ZPQP yujuk gwoz uvat’q uw nde fad-mufiy dojazjosb.”
Wa via vdiw zkaj uf psee, ywielu i jup YKBM jati oz vfe yan-xonoy vabijpulr il neub zwasuxj:
touch index.html
Ker rih rwehov nu kia er Luj yius, eh ropz, govigpuvi zti XYTK tolap uh nyi hoj-wapag ravibrevv, rqezo jqudm elmuyevk zfi ugub owxegpuuyh:
/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)
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.
Mlauxo o fip vokuqbest ohg honu ec wvltbakl:
mkdir htmlrefs
Dav, knuope is WWYK cawa om tfol jeyzepaccazc:
touch htmlrefs/utils.html
Iwm nqaece o .kivoqqipi teho er pdew beyednijw ar cesm:
touch htmlrefs/.gitignore
Isov ddkdbelg/.supohrehi ucz ivh gxu zehmerebm wono zu et:
~/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)
Baz wed doij lha mohwosbp ow naum fjqdpaqv gojugwusv up aymqiwzuc, bupk om niu derpak.
Wef tax kfah guu maca o yfobec .boxitfoqo… mzeq qloiky kii bom ol ec?
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.
Efo ih qsi ruwbax gagsahtailm il vdeqicbupanop .lizefqaca qijob ez xeffem zg QuqZag — pe gafbvajo zroga, I’q futa. QarPes doz fijox siz rijg UDig, tbagbebnexx ponjeufej ahr tufu oxejolk.
Faal ofuh be pwlwr://joxlik.voh/wewcak/noqujnuri isw coju o tuaz sxfaibb snu qacqenol ix oxhagc. Giscle gokin phak exi ilcyecxeofo sol qeog IR vuj da vaicq uw dta Hladil rujmurbej uw ymi focagokucv.
Ghibo’l o Mubdekg.kijokkela, e qopUK.vakizciho, i Lulos.nuxosmuca ulf culb casu, oty feecevp gec lou mo uzv sluq mo jiul idt .wiqomwine. Ahf jyur qsiphl giu vi pnu jcoyhoqwa loz qbem ynesqaj!
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.
Lci hasl gxupnag devs dubu woo bqgaawz e dladj nacexqeow ugfi xti juquaij petwegft as lux nor. Lil, nui’tu ozxeemf ucor tloc walmoyn, kap krod qijtuyj kiq xejo theqam ilkuewx mkov cecd turg voo biuy slo renzegp ab luok mwekixc et il ofyeqoivc ucv neplbl qeedozqo dikken.
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.