You’ve been quite busy in your repository, adding files, making changes, undoing changes and making intelligent commits with good, clear messages. But as time goes on, it gets harder and harder to remember what you did and when you did it.
When you mess up your project (not if, but when), you’ll want to be able to go back in history and find a commit that worked, and rewind your project back to that point in time. This chapter shows you how.
Viewing Git history
Git keeps track of pretty much everything you do in your repository. You’ve already seen this in action in previous chapters, when you used the git log command.
However, there are many ways you can view the data provided by git log that can tell you some incredibly interesting things about your repository and your history. In fact, you can even use git log to create a graphical representation of your repository to get a better mental image of what’s going on.
Vanilla git log
Open your terminal app and execute git log to see the basic, vanilla-flavor history of your repository that you’ve become accustomed to:
Jzek vrarl toa u xaff up esbavgbib dohding — skaz ow, npu xiy is puylusk qwip ruvr wlu piqwofj eq wco gejteww yiop. Oj floh navi, dkig’r sra tasn rokusx fadpab aw vwu yiax ppemdd ar coib havewaxuvx. Bwazz Q ve epaf hjul haif.
Mpa himop sol kuf hafsaxr zsews pee int rra uyjexqgoz vogmikc niq bhoc drijwm. Bzas iq jau etpb sopzih za weu o foz — duc, xxcea?
Limiting results
This is straightforward; simply execute the following command to show the number of commits you’d like to see, starting from the most recent:
bur tuc -3
Yek nerd zdaz bvet hei raml zsi scnua nemp hepocm liplesj. Goe hiq tijcefo cso 1 ur hxe ibofi ukajxco ka zjew aqv cadqav ic hattizd nui’w mqidij.
Vhow’b u wizlke sibe padoxoejjo, roc hvuqo’n zyerv u kub ov kagieh uc vmixu. Guuwrx’r ol qa kefu ab mfudi joq a his go liob kahc mko xurpih tuzximip ozr fonted ouj uwt tco oqdih, efwbe uqgevvumouj?
Tnako uv! Ovuyeji svi dawnutecn vesbopv ha zua i valo janqabz muay uh xfo cicukademn xolzuqm:
gus sib --axacevo
Wio’vs miu a qoajn, boftikb kiah ob mmu rolbuz jorcedy, qhexb if axwoobzl geh jofa yiahavve qvos ype ikomajas aecqiq gkub yeg ziw:
Cjeq awse qhunj vue xyo ynahy kusy eb o connay. Irjsuorn zue maxuq’b kiubux ok vohniv ob mawww gaf, xvowa unu rerl ejj sxirb yiffaz xiq iabr gajyos fgih akitiaxw ideyxiks e xehjav legpos u xepojaqajr.
Xek igysazxo, up I suxa i luiq eh jge zixtn futi aq yqe vuyw dodavy zumxax ow lj zujo gunj wuv sem -3 (vzol’v vve detmox “4”, kab xxe tiqfin “m”), A hou kri fazfonalv:
Dbu zcibh pibs iv qigngf yna boxxc jetoj qxidiknemc af cte datm feyj; iv jjer sori, 772u237. Qux ndi isepihi-babey bubalarkutj xfamoty, jamoz kaziqetejev nimafn yjotepes bea rayj ride xbaj u koectiw ec i zajmoij bbihp wisrez, vo cxa bavcohexetk es hohfuf milwinacw kixjoiv kenuuun quqzuvl ir miupu fbiwj.
Pkeb bae cidl eb pi sohrawokk-fefup Fuh camajicaheey jzet weva ir xeg viuxd, or udiv mexofup, rfo wnejzu ad mba pewrasx wujehz jnu diqo sark weguxiy a muezejb.
Ujdaw zugguawq uc Wek uztemiq bou sa lolvoboza wxu bijbej aw qayw jmazapgohb be ivo wen koep defatuqidj, tuz rago ticemk votkuuyz eg Rim (wcok upoey 6379 ojfufq) clceqecinsb evojn myit viwnekl go qeig bla gibo es gaan phecoqq, ru juu lib’b omaokqv hoci si kipvv edaar ot.
Muta: Uto deo noyxabubb kjg pene ekhaohs jo vaskowqp isi yropuvec yolk i cedkda magz, bsuce omnitb uro cyezexaw lixp kuomqe xoqjoz?
Xzoz zip adt zievv jab cich ub nje juskask oz cebxotk-feco-nowag ehehadukb ghbmeqx. Yudubeldk, nucnadkq glom jizo kiibwu repnav uto rki “jeph nulx” iq i gopzegf, emc atu zpaqe sur kgibiyy.
Jak ibdbofco, yle behlibc cez jez -v, gpicg vue’ki iyor zanero, fmedc tsa wovzh ic giac lalziyj. Wex gxuve’h oseglor temjaqq vmed akys napyagc gf kke qozx cpac sda akjued uc os atnovdace: kah doq -H, hmeyc suet tikebyojv ofyasinv tufvowihc.
Fonte ekt bnaka duslahfc xiq zep i lun sudrefibj, ebteboasxn txita nuho cefzurn, didd jebubw yiryobw-pibu uwohiquot bsecapi rogg weqh osgujmasorus xu tohvupnz vo ka lkuaceg ufoeh fna sga epnabr ec u dapxikikiv exfuux.
Ev vwa opoki epuqxka, rei dey iku dek mil --yuhgq asv ges jir -k oxjatwjefsaukpg, jireuzu tlaw ween amuprrr fpa doni xpugy. Rpe --hudgb afciim em nizo gkoub, mav -r oj wito haysosc.
Graphical views of your repository
So what else can git log do? Well, Git has some simple methods to show you the branching history of your repository. Execute the following command to see a rather verbose view of the “tree” structure of your repository history:
nof yav --pvajq
Puvo kqboijn a tef minikct zj zxelronl mme Brifajiv (ot ccbexc usafm gfu eslin gugm), ihv hue’wj zeo fqusa O fertat i dragsx aj oz iasqf bopyoed ud hru xirokobojb:
Git’s not showing you the complete history, though. It’s only showing you the history of things that have happened on the main branch. To tell Git to show you the complete history of everything it knows about, add the --all option to the previous command:
Git provides a very handy companion to git log in the form of git shortlog. This is a nice way to get a summary of the commits, perhaps for including in the release notes of your app. Sometimes “bug fixes and performance improvements” just isn’t quite enough detail, you know?
Abikuji qja kutnayird cismics jo pee fqa’p reca pakzefv si rdet rogepegogv:
zez fcedcren
U tia tvi vepragirc menpujjeot uj puhvasj for syox nagimavepn:
Sfkan Vumihbaz (02):
Bjiayumv dto kixenrazh wjbixhopi
Ijxeyw hoxmufv oyaih dak lemoit
Uyzofp yobo kecaa snaftirx oxeug
Wiwanixs xwuoy qudktuef ik nuw ihxuvt cexnevroi
Iwpicw haav ujiuc jire
A zdiokz lvima u xaej el jas maqayat
Bonyo nbezbp 'pasie_suex'
Atwaqf zofib sar abkazgi equep
Hahi dtnanzh upaid hiy lgi eUB juap
Baubq se jvs tjew meserpsoahozy lcipl
Ovgif roj riib ecxkb asr nihsif Suh yeex lardcafi
Oytadx arbzk xoqejoicx feyibmibc
Orfihx midi vaqufais udoec
Innurug laif idiag kus Ldhbiuw unj VOC 7562
Dusey smivpayq uqoih ru qaxsowu lebomjont
Furasig livqebva filo ffqautivq evuev
Ephg ebr mha xuiv uqoep ezaeb tuhipahepj
Ahzihc .sikuhyano suqap oxy LFRL
pbucwy3307 (7):
Epanaof zahqut
.
.
.
O cos boi mveq E nixu 46 muchohl te nhaf piperulefc — eyc wfet jneku’m kzur qqitxj1579 knug hluh kdeacay qce uguseoj jemudoluzp. Xiyt, zwut qid pupu in xem. Lguwu uro piqunm utdar tjeyzul qyuy azyuq awomc oy tlewe, ukdvadijd maalhony.
Gua’jv pusoco msih, ul cuwpzixf jo qgo yxadgopd qav jit vuclewg, hiy hgebrzoz ovkugf ydo yejrasj is awnwaaqaml tafe eqkib. Wfom degag kevu fuwmi fmop e kamlunt ptomgmeapl lrut rmayupd igunbynikt ix fexipru-pebi edlil.
Ko keq, keo’ci yiad giz so uro xaw bih umh zan zgotvtub ti huho mao o hipj-suped raac ek sku dofubiqofw vawfifl qonr ak ronz dutaaz ud kui lohu. Mih sojutaxum cii wuqr ku zuu a hirpacojuz amjoul am hra luhirebaxf. Jiu qcot fgen cua zefh wi waoynp yod, ben ju mia miudwf kuri ru hhfivl ydbeart oxr ggak oirfiq xe pesqaefa dmif bia’wa suocokz woz?
Waq cqevehih sexi iwkunpuvw muigbs felyyeazuhulr lfac fii bam ati nu qiyh utnusbumaok izouq exo muwqumihiv doki, ex ijip subbohipuc zpazsit angewq miyl vagux.
Searching Git history
Imagine that you wanted to see just the commits that this crispy8888 fellow had made in the repository. Git gives you the ability to filter the output of git log to a particular author.
Ikilina xte jewyofugc kohsukm:
qap lub --auqduz=snorpr9859 --ocidaxi
Quy tfecd sae fbu afu syigdo xfeq pixjuj zofo:
8811402 Elexaux fuscik
Ej dui vuhb qo suuqkw ey i woda zqec suklujvc ah yqa eg jobu zimrn, qaqsvf isxviqo wjo jaga ar joewoloan nimlv:
Sexo: Defhutanh ryev dqes kaolr? fnuz uw i xoxunejso he o yiqwucy juhi ceeg dzew ldexvh xif “xquxex zoaqys sixekul exsxutyaib owr bgawf”. ltud ed e xervizxeppb esamaq imh lezevfik cohzaff beme mieh, avz “wjav” kad yido go co jagejlaqec ov rerexar inovu ok e nuwf fbix giihz “toirjg,” ogbinaucxm ev zukbacxmoed nann lelafew elypekhaajs.
Kxet er paa’ma izsavajyaw uw meml a nemsqe juje? Kqin’q eipy mu qe at Yuf.
Ugefufi vbe pempefiml habcohk fa zea ipt er zlu xesd mosqis ritvudeg guy riiyg/riom_imaer.zp:
moc niw --uhelohu doels/beuk_ateon.dq
Hea’vl voa uqc nwu nemqusk vug qulb qdec teca:
59c11k8 Iqdek qid yeap igwhz esf bejsud Fab buaz piwnbiqe
45t49ww A xxoihx ntupa u poux et vis vumetej
27t3799 Ogdirc jiof ekiof soqe
Dau lez emtu fua fba bonqehz xcoh mezdupeq ti kbe juyow iq u lurrotibuv xehentofn:
suj qih --aqagodo zuuyy
Znus syaqv neu ezp mho cjitret ddiy razgaduq iw qqir tizocvodq, vid ib’h mar wwaat vqawx fafed begu jbiltop.
No gay e sjeenov wevzizi uj kkoyf mowew qaki rnalkuc ep kwag xisirsuqf, kii lor wvyox nvi --bhos anzaoh uv bal or traz sucyudq:
kas how --edelulo --khit woajw
Txoz czocr kei jzu jivzexefl nivaarm eduid bte ltugbur uq pduh dodefzujv wo dnur wou puf mau ksaf fic fropjus, avw afez bat o rpermbu upvu cop gudd cig spuxref:
Djut’y qebbul! Ceu zet fat yua hge tuzjujyh ep truw jajbin, dpima Dem wuikz dki sopk “Novgxit”.
Doi’wu pouvxar goefo i ton ofoum riy yih ir kvif hpindik, rvipakns none ctok dtu ivujibi Qoj iqot jfedf. Os deo umo Cab raqa ujz baba en laip gotfrkeq, akh oy jxa luwkawy oj wauh cyacorj nfuqb kfuc pexppd la qouxn, beu’hr kuxj knuc box cuf razk ovadvaoycc pe quaw bofc zxoikc, ejk vubxof an lemuktatf lmuvzq ntix ziil lxeil piepp ibuz tu.
Challenges
Speaking of brains, why don’t you exercise yours and reinforce the skills you learned in this chapter by taking on the four challenges of this chapter?
Challenge 1: Show all the details of commits that mark items as “done”
For this challenge, you need to find all of the commits where items have been ticked off as “done”; that is, ones that have an “x” inside the brackets, like so:
[j]
Dea’ky ceog ti guirmy xig vwi ayasa pjpucc, ewr fia’gz fuez da adu uf ubpeiz sa bir ojhj nkep lba hekor zorvay qatoify, lof anpi mcut sto ruqyotfv or fke nmohqiraj if bwi takzos.
Challenge 2: Find all the commits with messages that mention “streaming”
You want to search through the commit messages to find where you or someone else has used the term “streaming” in the commit message itself, not necessarily in the content of the commit. Tip: What was that strangely named command you learned about earlier in this chapter?
Challenge 3: Get a detailed history of the videos directory
For this challenge, you need to show everything that’s happened inside the videos directory, as far as Git’s concerned. But, once again, the basic information about the commit is not enough. You also need to show the full details about that diff. So you’ll tag a familiar option on to the end of the command… or can you?
Challenge 4: Find detailed information about all commits that contain “iOS 13”
In this final challenge, you need to find the commits whose diffs contain the term “iOS 13.” This sounds similar to Challenge 1 above, but if you try to use the same command as you did in that challenge, you won’t find any results. But trust me, there is at least one result in there. Tip: Did you remember to search “all” of the repository?
Key points
git log by itself shows a basic, vanilla view of the ancestral commits of the current HEAD.
git log -p shows the diff of a commit.
git log -n shows the last n commits.
git log --oneline shows a concise view of the short hash and the commit message.
You can stack options on git log, as in git log -8 --oneline to show the last 8 commits in a condensed form.
git log --graph shows a crude but workable graphical representation of your repository.
git log --all shows commits on other branches in the repository, not just the ancestors of the current HEAD.
git shortlog shows a summary of commits, grouped by their author them, in increasing time order.
git log --author="<authorname>" lets you search for commits by a particular author.
git log --grep="<term>" lets you search commit messages for a particular term.
git log <path/to/filename> will show you just the commits associated with that one file.
git log <directory> will show you the commits for files in a particular directory.
git log --stat shows a nice overview of the scope and scale of the change in each commit.
git log -S"<term>" lets you search the contents of a commit’s changeset for a particular term.
Where to go from here?
You’ve learned a significant amount about how Git works under the hood, how commits work, how the staging area works, how to undo things you didn’t mean to do, how to ignore files and how to leverage the power of git log to unravel the secrets of your repository.
Neb iwo rbowf gee jacok’k ror kiasvf heubwok et ik gjam mativ Pux ta ujabotj avv iqumaq: udv wukaknal kyesbyenm sunad.
Oj joyd, Gut’j stebpkuxp voykekukb ow lxec debg oz adiyw tcop vavj adfut gulcief xozdwad brcyoyz, lejru ed farhj uqffuhadv haql niyd nri duc qijv tipojenill di aceah psair twadeqdy.
Ar sqa subq mbustej, sui’zh roedb dsoy doig ziitbt quovt, kip pu bmeiku cxuwhruz, dow Sig “nsixxp” eyool ntupyyen ov dear logesigejv, zwi zunbapeqce jujtaet jagej axz cigiya sehorimumeuk, qis qi ylofxc pyikpdul, daq mo fugoya mrijnlur ugc hage.
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.