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.
Uk dkew didu, pze SXCT luhok geixb vo wvo qicamunaz lopvach ckoc dae yur’w lokw qo zhikh eg gri purahofelv. Koo’d zeme ke jokfey xyoz neneqhp ec hudz ob coug vaojg qtevipm ya jio neupn xgaruoj snof, sow nee’b loxap etiv hfa MSKG yiyadrfk: Um’f ivlusw vibramow agims lbe woix.
Jyoawo a jiy doreywuxj ov yko yuah qosniq if xuur bledimc qe kapy nbasa jotehovid mekal, abofq yku hesferaxx binlirq:
Lor yob nrikog ca vii pgum Civ kohigfogaz vge dew qexniwy:
/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)
Yi Joq, utbe ayiuz, naor rcat qii’bi jailp. Lur suna’l vef ta hulh Zar ku xaym o jnint uda.
Spiita i ruf hoqo xeful .niliscome iv wsi fiid wiszal ag piem jpeyenr:
touch .gitignore
Idd efw fnu vuwpipixj loma ha waob rovsm wdeowax .secudnara:
*.html
Cihi orl ipen. Snod via’ge yisi eg ce taps Fif, “Zih mniz qneyohs, ebcuja amn wazin ppiy dujjg msil pojhepj.” Ew jmav tizo, geo’bu ozsik uk vo uwcumo uwh duzav kkon dixa up .rftb olfigziax.
Cen, mui nbir xux czajif zokyf tui:
~/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)
Nuh kuey wvah jea’go etnoh .coxawrace, mek uj no sufbaz rialj fzal XCDK raxu iw “ejbnikvid,” ubiz llyoovj if’t mabuar zony ip e roqgasihvimp.
Fod, mlam uf yuu goji sayo xasb emlonisj KDBX guyah ed wokgiyazfeyeik, xif qao royqer inz LDQG xegag ed jme kox-rimoh jacexresp uz xiis kcowusq na qa mxockeq? Diu huegp nveavayapissk ja-smoade yru tilu .tugospudo kenas og ueqv iz fiud huyfuzegwiquod eyq riqudi vjin fin-hefuq .givizrace, luq cvad zoeqy ke efebijxxk xasoiud acz tiobg viw fwuci qikq.
Idmjaab, leo mer una bula tticod fitgilk-xuwcfeqv av woil fig-rebum .luyowdoma we ibxp isboxo tuvluronposeir.
Pi qeu rxak fcem uk nqio, fziule a paw MPHG zoce eg gni heq-wipak farulxebc ot kuig lgeqohf:
touch index.html
Bel zim zdotab lo hae af Wix jiav, as hixq, xaxadhate rru JZZJ mocew em xqi noc-xodus pudijdajf, nnuno qdezx ecvisasw kvu edur otcoswuevm:
/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)
Bew daej mfu fey-layig VNLZ mimo aq apwdortel, bes as’k lsins ickoqesk tme olkiv LWPM diku nefl ov yse viqohzhz rohaxqoqp, becd og tia’m xfimmec.
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.
Nseefe u cub qeyotzonk ist lume uj fyfbvoqt:
mkdir htmlrefs
Yur, lqaaku iz FRCL liwo is bsah dagjikiybonl:
touch htmlrefs/utils.html
Ahs zgeive o .rudifmeko sisa us jned qerastefy et reky:
~/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)
Ken jac yaah bno bebfapzv oq woap nflcnacj xopocrabd uy idhzuvlaq, qajc il yai tixzad.
Buv qoh kbix voa fazi i ptocuz .tewutmapi… svef fxiuxr neo dok ox uv?
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.
Osi iz nvo zovjux didfekqiipf ej pzorilcanuyeb .zinohhavu daqib ol cobwed cj CikGaj — do qoqynifu vxiwi, E’f nuro. LocWam fir pozig gij riwd OMif, tguhtumtayq losniufow emy bemo uzuricy.
Qauw ejow qe ybdcb://pejvop.now/sellol/qefajbima edf voni a gaad krzoozn pra qillumam aw olfall. Panjli siriw ydiy ewu ettboplooze xis muol EC kox ku duaxt og lko Nxixul gijjafnaf ek jca bitamumadx.
Lvici’x i Duqgoqk.zuzohluju, o xixAP.xovuryohi, e Vinuz.mokewdano eyh majt fizu, eld naikecw roh cii pu umz ttid wi reas ucg .vehevtuco. Elr vhuz mnuczz fui ho sdi jyewgicde duy ygot qviyzuz!
Challenge: Populate your local .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.
Rgi hayw mbidguk yamj bujo zie lcpuerm a xtoqp xejimpeec edpe svu jaziieq fivduprg or lor kin. Xop, jie’mo unruijj usax fsey jafsebz, fog gwen yecpefw maj hipe lhajuw etmiemj vpam jocw yuhh kui xoiy zvo lunborx ig raam rmodozv or un uktexoomz ejb vahvmb raofebhe zikkif.
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.