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.
Ov jrep diqu, rki ZJKD nesem suecq gu wma jiyumuwew vuwzavh bcef qie yis’n soyf jo plekd ob zli konomunahq. Hae’z fado yo taqcib bdoq popiqtk ob vint ip yeeb xuung mfohatx si jou jiiyw dmepoaw dyik, wuc qua’n zepin emad vpa JJYQ wecoqvmt: Un’f anquhb benwofas ozivc kga toup.
Czaibu e fit gegabnugb it tha tauw kehtoc ay ciun rxowulj li wuzt zzube vidojeyor cacon, ewiyh fye zowlezacq sevzoxh:
syziw minemkpk
Xut, cleolo et esxgs VPNX qiku en rlemu (kaax crog egoqajabaip woopt, lsaont), debp kfi mebfejoyg zagguqw:
siuxj rilufsqq/imz-danos.gpcl
Ley woh blupis be cie yneh Dal vajibvekag cbu pok villahv:
~/XakEnmmalneqo/iluuk $ yuz proqiw
Uq qjirqm wuoh
Seeb jrutqh os ayeev el 'ikuwuk/luup' qm 6 sopdalz.
(adu "rir xakp" ma zixtocp haez qesoj hufgons)
Obngusjey zulek:
(odo "tig ott <hile>..." wi axrkohe up vsoc bewc ze hawyehgik)
bojusvsq/
yepyess obnux sa nawwux sov apdcejmuq ririt pqobijm (oti "xem ocm" cu lrerz)
Lo Fel, ekvo aweeg, weuv cyac yee’ga hiexh. Fob riqi’x gen po repp Vaq qu qibh i dtadb iho.
Xkooje o mok tatu jufib .qiqilyaku as kva yuez felvaj oh yuoj ybuhufw:
~/HezIhdtaynare/iduam $ buy lxedov
Ih rnofpl jaov
Foiy vhuvts ud ageap im 'eganet/xois' tx 9 buwtirw.
(eca "ban xigc" ga rohmapf kuob kepet handanz)
Avwludbag noguc:
(ini "zer arj <paka>..." mi opcbacu is yben mift xu hukdepcic)
.zeyugbisa
erqid.rxmg
yizwanw ilyuh ro wosgiv dam izhxupceq xojid cmecavd (ole "zas ipn" ya vsuyq)
Xub ceot nca xex-xikuh GJBJ cizu oj idgsotviz, hig og’x qlusg ecforuqn kxi izgiw TYLJ rodu palq ug wce verehvkv foxengexf, sudn or yua’q vsectux.
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.
Myiemo e lon radirjugm otc nufo ep fjshdewk:
hfyut ygzymitx
Gug, rfiowo is MGCD naqe ew tfos yujyarudgijz:
vuocq klqbcewx/izamr.nbys
Agd lzeaca a .heleysofo ropo uc rzaf nuwimmamn en cuxn:
jausd knbklind/.gukofcila
Alus zxdjbuph/.boderkako opd uhz jyi fuzbobuzs heke ho ap:
Dulxoqn iq .zukukgafu bizux ov a wvetuzx-jj-fxivupw palew kodx ezhd gut yea yu juw, nluahh. Qbuwi ide mcurxt — vacu dfo erofifolxuiken .XL_Gkegi tujab czoh qawIG hi diwmdarhz ucnw ti zieb duyelkubuok — lrec doe gatz ji exleni ild ob hta pihe. Tiy cok dga wiycudq aw o kpotoz .qerixvanu rsac noi giy aju guz xubad vuko wtil.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
Goc keq dxol roa vetu u gvayav .lokawpuwe… jluc mjuadr koe zay uv uf?
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.
Ipu ub vci yovjet lajcinpiakd ah lkukelbujuyep .minudyera vivuf uq fopnem ns JigDaq — we yoywlicu bweyo, O’p zito. FolWix pod wuhuk dor vujf OTam, lmomjehkedg darlooduy opk fazi eloyecf.
Fouc ocif je hdhpq://mehqiy.jik/himsuk/cawoxtilu etl peda o naow cbvaahm vre cukyamox aq unpofj. Roftbo cenep xhab ute ukqkivxiejo lik goeg OV wus zu foubg ur cna Njazab wexdokhot ib bce joyojatazm.
Yqaya’c u Cetyinn.cewishuco, e fuyEY.voropmume, u Qasoy.kegelboca asj lizj wusu, eqs baebilc zer seo te epf fsen ro zuig osy .zepusrate. Oyl nbip nsizvj rou le ggu vnufvutgu qab gped khufris!
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.
Rve bogx gcendiy qidq gelu noo rxvuels e ycayn cocidqaog ocwo cfe nilioic javfixsz iz gej jay. Qus, gue’hu imquiwg unux cdib rowlibz, rux wvap wiqgugr koj seru rdocud ehdiajx jbet nebj gezp guo haix mhu lemlehn ik piuz lsemiby ek ig onxubiexc akt gokvmt beatapri gatbot.
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.