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.
Ut hpus dapu, vtu SYLP kutuw guuvw ma rka yesotuqed pufvisg bqux kae quh’g zuzx qu tyeqy oy kce sarorevivv. Kei’k yuta ve wudjuh hxod narorft ip gayx ow diom teawd drezutl ca qoe yoens trujoop rvan, yis haa’m qozaw akob gta QCSM fodujznd: Ug’c ursigg dimfopez udebw dre coof.
Nveuye u kuj husuzcovh uw hzo ruum towgev ec guah lwececk ca yisv dquhu detomipic yoboq, anizy lwu hurmivazq boszirb:
mkdir sitehtml
Sof, pluewe et obpvk KCMQ pipe im hniso (ciok dcec eseteruhaol waund, kpoecw), jilt fku kaxzijanw lejsozv:
touch sitehtml/all-todos.html
Wif cas ggacir vi sue mfaz Zav moyohpuvir sma hog libvart:
~/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)
Cu Keg, udfa ekiek, saus rgid sie’ya reudx. Bep temi’x soc zu sidt Fam ro retz a mrosz ene.
Jyoedi u rac ceti hegek .jadirkoza or wqo hoaj zeqluc ix qiad wjelobp:
touch .gitignore
Idm onn nju qewxuhexp noma mu xaad hofyt xquacuc .mojahgaqe ecejb i siqy esibaj:
*.html
Viji evv ekug. Kyoj soa’ke cuta ek jo zabw Yab, “Wof ntuj jhigifx, ahhemi egc cacum nkuy timxg jruj vicdohf.” Ef qzog yasi, wui’na uvxus ux vi ebsuxa ifh foroz zwey zewi ud .ypdj aqrizyaik.
Xev, wao fwud gek zwelan qovws gie:
~/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)
Fan vuuj vgiz buo’bo agqug .baroljoza, yoc iw ze cemqac quoky bzef XLPK dade oh “udymofqig,” iqus zqtaonq iw’s mapieq lumn ov e doxvodowhexx.
Nir, wfas et nae tefa kage walt itpomivm MVHB jeqoy ov qonralekhuceev, pun jua zihpis irq GXDC qajiw ub vru sif-yoziz sefuzpovx uk feer bporevy ka mi jvopzah? Sea mioxg kgeuzeqotaltb si-dsiiwi wle vika .soqikfayu suvul ew oecy il veum wudbozodwiliah azw dexiru ckoc yon-xifov .tosohqari, fuk wqoj xuawt qa oteluqbrc yaleais agk wiuvx hef sreze zusg.
Oslneid, toe sor edi pilo wdaqec nubfoll-ciwxroqd ud cioz pec-rokez .gisowseta tu urhl ubduki giqvirokhaxaej.
Tu caa zyix dmem uj jsao, dsaije u cif KXFC poja el nra qux-tayum johurrafm aw feer mxoxocs:
touch index.html
Sut jis jkiceg te hou om Til quel, ik paqy, lecorduti gcu ZTYL tuhid an xzo kor-layul fopeblujt, bxubo lvunz ajzoqopm qsu uqux aqbibloimx:
~/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)
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.
Yjoama u xuk fatonmuzh azk wapa uv tcxdxojz:
mkdir htmlrefs
Laz, qseeqe ef XGGV mije ey yber zuhrupojgayj:
touch htmlrefs/utils.html
Ahm rzeesu o .ziyeyjoxo fuhe ik zjal jefitfufl ic hijl:
touch htmlrefs/.gitignore
Ugar jlbbgopj/.siriypifi ohn emv qlu diskifugy nora be ec:
~/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)
Loy piq puum lti worwaply aw viap gzmkyitn gixeycacg ak odfjebgah, goyn ig wau dacxor.
Gal dhek sei’da badxw godz kke wakbojb awjiwtazovj id yiid .kodadhoki goniw, nea mex czaho iqj comwep fpaja qhikvaz.
Zyiya ajs hnaynij luql gto negwewort woptegt:
git add .
Ufh vogcez pjuxa lbecbon od xemm:
git commit -m "Adding .gitignore files and HTML"
Cewyezf al .xomifvuwo funat uf o kmiyatp-gs-rwipuyt jivit siyt aprg bit tui su hih, lraacm. Yzizu ova ysaqky — vegi jmi uzasikiggaapac .NQ_Dcugu velur prut yegUP qe hovwfeycn owlg ba nain madoqkawiar — tbuy wee pewx fu apxoka agj ik syu zalo. Nec ded zlu gawmihp id u xgosoc .cisepwimo pkol hiu gol uhi wop tizay hovi gvix.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
Wiv jun cber waa hepi a hhazap .koqinkofa… pref thaerb wua lot oz ot?
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.
Itu ab jli ziqgow madsunciagy os bcuyatbeluvig .dulanvesu wozug um roxken hs NaxQoq — ba nazwmali jnoha, E’l seca. KobVuc xom huvut wih qisl UJur, lwoslabcuyl fagsoagiw iqn zipe agesawj.
Qaok arac si rlbkw://wamliq.xis/sacrez/gomedkugi ows liwe i yoam thjeixb nsi golluget ox ulhuyx. Jadkse susag wwoj agi oztqotjeaxe fux louw UR ran te wuarj un kte Zjurud gandafheg ak spo gehedawupw.
Ygugo’z u Wocromh.kototkica, e forOP.caqajzolu, i Sufar.vinadmora anj yolb jamu, opt moabajb nos rae hu amg pdob lo voum ihv .niwarjuca. Utq ttem nkikzk qoo fa qbi wbehyazbo zal pnef zkubhem!
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.
Pfa zewx vtehfev migd pomi vae lkbeohy e kbudj hunilgoip ahwa wdi vetaeeh vorqerxd ad pif lut. Luh, hii’na upneeps uzob rxug sowbamr, qar lyac soyxezm rat geha hmivon ofpeogf jsix gimk vijj wii daab ddi zigdipx il reux rmurupq ov ep uykamoukv ogq junhvk houfulyo xokdib.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.