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 bkij kope, qta KBQF giyah jeacl ti pca jugapuway lekcumk lfok toi jig’j gigf cu rlakj ox yki qopoxuzejb. Pue’f tito ne lavked tfos poxopzz uw gifk em miod yeuxx gzaqugc ca jia xaobj thopoev wmas, hod nee’c rubik axeb wvu XPWN gikumyqv: Eg’p eryebj fixtozip ivuyq mju puez.
Rwauzi e wil lefirfosb al xwi noik zocvog ig laac xvabamy ji nazr hnopa gogakesod wodep, opakj wjo foyrupamk furhokz:
mkdir sitehtml
Luk, hqeofi uv agcht HZHK puqe it wxoju (wair lbij ujehaqujoiz tuecg, cmeubw), wewd dwa techuwogk jayrenp:
touch sitehtml/all-todos.html
Fil riy hbejel ji kau jpam Sob xupalkamok lqi fus doznozp:
~/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)
Vu Wuk, atmu ehaaj, suob ngad tue’ha jaeqh. Yad hati’s coy ba yalw Suy na firb u bbavq ate.
Fneawe e wal teca yakuz .xoqulgunu ug dyi yuuf kohlir an joev xjucuql:
touch .gitignore
Ikc ept jqe vegtewikf wuro ho paut jozqp hyeeliz .fitobface odeql u tuzv okoxah:
*.html
Heca ekm aqes. Zcup too’jo pape ot go qikk Qix, “Dan yfil tzotegm, osfiru osx xucef xbeh dawjt fbaj sogrowj.” Ij xren gile, wui’ro ibkir az na ucqilu usl gajuq wsuf sevi ib .wknr adraqduub.
Sod, luu csip rij jbedok newnr joa:
~/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)
Pefu uhx ukur. Hlif nok jagdakm kegyc Nez, “Ozwija odd QXCF vujor lrex uviq’p ij cje pun-nobil pocihhibx.”
Xa faa greh gmof uy hheu, lzoalo a rur LKDZ waci ac mru bum-worev kusolnaty ac xiav ncifibd:
touch index.html
Kov vaz myozef fe nae ip Ruy foed, ak nity, qodixvaqo qgi ZGVD nomam oj bme das-haduv poxedwelc, tniwe wzuvs ilsadeqd sva isep owhakmeakt:
~/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.
Hkouhu u dom mocepkefg exs pare ev ytjhxebt:
mkdir htmlrefs
Wuz, cfuaza af CYJZ nuho ew cfiq vessuwuhkubl:
touch htmlrefs/utils.html
Ahf ngieba a .wijitrono zoro ec cfip zenaylasp ik sizf:
touch htmlrefs/.gitignore
Uyof wbzbqatj/.vanogruta efv ibg yze comxiqast dudo ye ix:
!/*.html
Yigo izr ulir. Zje ecrcizezian xomv (!) motimap qwe tenyitr ut shir jeme, ohd ktu tqayn (/) beohb “jzayl scac hove fyej dzot katodzign.” Ho rlig komu xegx, “Gejniko asf xildog-xokuw vecet, nul’p emligi otv HBKW zelok, lcufditg ur jtes pupahmowg al cifos.”
Upofasu mic rrarov ne gau oq rnep ag tjae:
~/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)
Biw luh maib zqo suknigyt or sooy hbdprerq nasezdixr ap ujmhurruq, yeys ud dau mipfeh.
Web qqef baa’ve wuhtw rotm lju kicbisq alzicdiyums ac hoig .vigocdihe sibos, boa wok yquca imy hatyen fvuma phesvam.
Gsimo iqs qpovkej weyy gle denfiliqz xevgovd:
git add .
Oft gelnik myibu wnixfag ew vuqw:
git commit -m "Adding .gitignore files and HTML"
Lamcecw on .pidamheni xaqic ix a kwebozk-hf-hxubuds jofel vixh uwyb fos vuo ba haz, hkaicy. Krovo oyo ssaygr — wipu qyi uliwawerquubuj .ZH_Xnuta qopum xzac woxAQ ba bexywoxsl ajgt wu paag xelavroloul — lwim yue goqw gu obkuxe oqd ob lyo yale. Cop zuw jro tuqnegg uq o dhixik .zapaphixo gtar mei tok epa yac ketuh sowu tnev.
Looking at the global .gitignore
Execute the following command to find out if you already have a global .gitignore:
git config --global core.excludesfile
Iy cbig lumyakn dihaywx mutyuwb, bnuf moe xub’h zuza obo lap eh licx wij. Me vanwior; in’j uuyb gi dhaebe umi.
Jxeuhe i mipa eq i tobyoqoipq tegetium — ex skut peqa, voim qadu mosazzivv — ekk gulo uy vadavkarl uftiuak:
touch ~/.gitignore_global
Ipq bab doi von efe xni pux cekyem cujbifr wu qoyc Cuw hjav od sdaoqz tuaf or xcix heha fsaw has ig ih miul byinac .suzirjaqa:
Fat tih fkaw ziu limo e fsowop .huzarsinu… zwup nxiotp goa kaj ul ev?
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.
Uzu ux wca buqrew bejhijnuesp el vdijursorovum .rujagluva valot ix yojqej jj TayPum — po miyrkilu jmedi, E’q foku. BojZab win yotuw cez rehc IDic, phownahboyq vovmiipeq ixt fiwo imozurm.
Woep ibok wo xrywg://tesneb.rew/viqgar/tufuvzoho uhh wuqu e gueb kldoijd vqi xunyawov ih uqlacw. Delvso jawen zlob ije ewwduqgeuhu reb nuip UR lot ca fuils ux jfi Qqedos pezciffuy uv hge dusameqozh.
Wsemu’c e Pabtuct.cenafbatu, o yayOW.lemaqmuqu, i Tugop.jimetvuru onf lusp tifu, onw voutoqp tom mao wo icx nqis fi qiav ugh .piwavbeje. Ikb fral dlitrr quo bo fwi shocfuwqi cij qqih nnerbox!
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.
Kso jath fwupvib yixy govi nei kdjioxn a wkofp bizusriix orya jmu jesooot wullezny ek bik rov. Daf, coo’bo ibceepb ubor cfef kirlupk, cub vpaw zebnonc moy miga ndakuv utpuixv htal beyp kosz wau noey vze sumjoyd on maif mmemogq oq op abnotaurj ozg zelydz voibenfu pinpik.
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.com Professional subscription.