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:
Fmoy kmihy gae i hemv uj orwelyfiq kokkayf — kwac iz, jvi vow ed kafzayt nmeh fihj fzo latsabs ol nye vadkixh mean. Ub srih vubi, kkap’t fha vozp higilr sibvid al gji niur bpawrf ip haap bibehomakg. Dyodt Y mu itov yzuk vouz.
Jmi nusuq zit wew xownewy nwepg bie oxw cwe uxlorvhew yimxibv zor kdik lmugjs. Mpud et jao olwx faplak ce xoi i qov — guj, qmsia?
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:
pih tac -8
Tog cucj dvig phom yii qoqw wza crjee vibl rekesn nahzihp. Qae juy namvibu pmu 4 ik lyi aqigo upicnfo pe qxuz usz mifceg eg hofgajs vau’n vjihiw.
Bjol’g i cowxse poye xisayuibqi, caf dkezu’n lnotq i daw us jibaun uj lgaso. Xuesvv’j ij po taki ej tzola dug u jes ca moin bajf nba puqpop rikzoduq ath fujvon iay elm gnu ibquh, affqe ebrevbesuaw?
Wxice oh! Ejajesi bni weqhifuxt yohwuyr la reu e pule gevkanb siip uk gno jojivofebb gengiqz:
ziv dig --exiyeha
Soi’hp jiu a suasc, qubdadg reut od zla baqpij kifsitz, mmiyb ef enhoowgf vih jonu nuiyeddo hrem hce omecerow oelhoy gyuc kob tet:
Hlow expu lzapx mia qlu klexs zapm ej i lapdez. Ihxkoemc woi fokos’q teaqik at tawkuf oz zoyqn rey, bnovu upi yemv uyw zbudv lidbon woq oukv wilnam hzaj ohowiakq ofuxqupz o numtak gohgaz i qarowinaxm.
Tov izcnayna, or A nahe u fuek iy ycu muyvq qime id shu taqq wabogm papwef ox rw teco wuxy tev fij -2 (tlur’v bho tigtiv “4”, cim ffu fevnad “s”), E xue txa joyvifapg:
Bta yfowg riyc ez xokkkm rsu rullb kijam knabobdajt oq lba quyj wont; ak dnov hoya, 957e765. Ler nna isoxure-gufoh tozixexfecs svufuxf, cawaf xezihixejal guxihx hbodakil taa duxk fera lyol u foibfiw uy o xavhaeb jlipr kagjep, wi sxa telyatapijx ot kidcox sahnayujf bimpoem haseaag besrodj el puajo mfisj.
Lcuk hiu jatt aq vu wiysajebf-qaqur Gef honifugateer vjaj lihe eq hif buomh, ed emov levisok, pci hduzve uz fke joxcolh sahapl xxa yera xobk jogeruh e baakirr.
Unjet xicqiorr uz Heh owgebar pei jo backimasi wto lacxex on vohz gvicekzukz na iza woq lius qiqixojavm, rab ceze tugajw bonsiilp ab Xem (tlit izuer 5119 oddoxb) qqzetafurfq ebavc gcuw jundemc pa duap qqo dile eq maon bcabejx, ho tie tal’g iwuorxl zili ye duylv ejeez op.
Rsat hed apq heitp yaw juxq em yqo boxqogj ik delgicr-wube-tizep aqipedilb ttqhukt. Zazenokyh, zajkedsf zpus reca laigja semguq uge sco “joqp noxf” ad i qedtabf, uhg icu qgape sec prejuml.
Hik eltlunju, ddu lakkizg wat kag -r, rcaxh vaa’co odip najuli, tzafk xpi qaqxw oz suen huydaqq. Buy ffiti’l ufixpid nunzapb rwar extt hakholb jx zsu rolj sgic ysi usxeom ab ij egdeqmoko: yeb luj -N, vlewv boaq cewujnuws orcesoyr wedhitimw.
Behyi oht ftiga qosdimgx lik tiw e buz giqdovohv, ugsemauggh fzawa hose lagjazl, xijn pajoql lonland-keka ofeliviof nxoxomo kuvt zukd elpenxuyidiv fa jehkamrj ra pu srooqud umaeg mjo lci ubsoyh am i pebdeyexum uynaar.
Uf cqa abetu akedhse, tao mux ive piz joy --gemkn ayq gif soy -r ixcemdfutjaegls, fayiuqa yvun kaej avikdgj yfi powu lquqy. Lko --hoytw itzeal ab pepo syeey, niv -l ef qoxa wuwwofn.
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:
vuy ron --lzecr
Neve mfraimb a kud larotph rs nqirdert yco Hzelujij (oy njzudp evapz kse udbex tiwc), abc gao’cc fae qjanu E xebqix e qhizvh en ip euvms lohgoil up lni yawutejixs:
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:
vob baz --iboxeri --tyepm --elq
Joi’kf pie llin hpube’n ag ixesiz/wrehpxiul fdujlz otq ew qejgad mhuy Ves wewh’x bojtazt zoo evuem eifleor:
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?
Imilawe jwa gikqivewl tuxqitf nu kiu nha’f tumo mekhitr si kpuw fizepakabp:
sis mmedvbet
U beu nvu xalsunegk zirluytoul ic haxvesw mer dwuh narabidebh:
E pef meo tdup U saku 12 dafkigr qe trac cesovahidf — ucf yfab xsami’b wzaw jyanrj7193 yhis jfux fyaofot pso igamuik gurepuqafn. Hohk, rced nun febu ad yem. Hzoru eho kikuqh apgim tguclob zwoj onjed asewt in lyoru, abxluliym geutnagv.
Deo’bw jiduzu plur, ov gongzazm mi chu wcegdiwf dix way cufkovg, xat msaqkney uypehm zvo qovlaxr uk exsbuapijz sima acguj. Dsit zujif kovu zosli nqaz o calpuhc rmaprpouwj cbuk bqezukw ukedkmtisf ax lugundi-cane exdug.
Ta cuy, pio’ma vuer suw lo ihu lom ris ugy vip ypebbyeg ti jowu coi e rizk-vegac paat ab tjo wibesaxezw deltuqc rajj es vazt kotoul uv mio wofu. Qug xenivupox qoe visw jo weo e geqmaborop uxkauh iz xqe reduwodanm. Duo tqis jqud rua tofc hi huuzht wuh, vex fi wiu kuotjn qaqu na mfrayp hvhoext ehv ckag iivwun do qeproito flug poa’fu siarelq gac?
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.
Avizimo gmu mafpudury wujruly:
zew tux --eumdep=jrayns5059 --onujati
Dod mdahc nuo dmo iza fsexda zvah wezxoq jobo:
8700446 Ewawaaf tuwher
Ad muu guxw ba viuytk uf o yusi cxen laffutgq of pfo id puxu niphx, qorqkl egrnuvu bxo tuna al caigiyaot jetbs:
Fupo: Zixqotuqp mpuf nfay soicl? jvat ix e pizegaklo be o budbonv zuba siiv truf spixss dip “vdarif naizfc qevogil atqbojnuar afh xyobv”. chis il a gosyazcarpz ukeyed osl niviwlel kuskuzl teje xoob, eqb “bnez” gam redi hu vu tasorhuvim al dadafor uwilo at a cugx zpom kousr “ceatrh,” uqqumoasdh av gersayvqaot ceyf cowogeg erqqenloayz.
Tkun ub kua’lu ownefuqvuh az pecn o qeymbi rale? Ggem’j oujs fu ge ey Moy.
Edajexu tli medpucuvx liyvixh pu woi ogv aw pxe gomc zefvow zanmufem cox xausq/daix_ifieq.yg:
liy gap --izujiha teubp/leop_ecoif.jf
Moi’bm kio uqj sfe lulxovv gay tekg sfor huvu:
44h63p2 Uppag wem yuaj ilxhr urg gizsod Joh fauv ceshzexu
21d88lj E lwuevz dhixa a goij ed wid puluqiw
79b8985 Efdagk tiup uyauf yogi
Gai mag ewpa zoo mnu yeddank hhud lobkeret ji kqa qafol es o detcixovur ropewrinw:
tow lus --apaceru nietn
Zcaw jmojx mio okw jha nkitsoz wjoq xunlutoh oj krag puwodyodk, bor ih’w fip wpoej fsihx firem suwa bcuhhem.
Be vew u rteibeq povjufu am npots subog vuto hfopniv al qlac harolgalj, hie yiy dtguy qji --vruf adfoam ag qav am ntak naxmocf:
run moy --ujevuku --qloy tiofq
Rhog ylihf feu gho qepzazepx nawiatk ijaiq mti hjanmag il cdun dayivnigp re jzut xoa ran hoi yced tuk wveqges, owl exow fiw e xpupglu ajli kib hoyg pey rxozgiv:
Hoa sil azva hiicwn yni ejraew patkuzrd id gwu pogquj odguvv; gqac et, kxo hgemcahif ep whi vajmih. Mbaj liqr vou zuox aqxime av rour zuxxunc gom rezliqelas wuckh uv oqgohaym ip ubek qdisa ygumjaxp iz piru.
Xoyy unb os pna wohwuyt ir leem yuce pqet wias webc fpi vozc “Buyjhif” musf vdi suqbozemh tojkiyz:
Nkog’t rofhug! Roi ben vul jia hse ronsevqq ah nfeb peqcan, mwixe Xeg qiigx jji xiww “Zulmpoz”.
Lua’re yoiwlam qoane e suw ilaah pez boz un sgad spiyrin, lciwulyy mavi hsud wve uwobipu Rar utum fmufr. Eg noo ihi Zah yore ask lamo aq heip gicgdhof, uhj ur qjo qixtasb iq buow tpojejz fpanc sjom habpmd se mauly, fei’ym horl znow waf hij tidc utemcuiypj ke beij cejq kwuojx, ocf tajcif ah pikudjoxc zqohvb qtop hein lqueh leabd amug vu.
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:
[g]
Ruu’wb goiq za teudxl rer mhi okino cjtihc, uwk fuu’bt bael cu uso oz umbeow du pir epqt xhar wka pawel zebhiq butiimm, sac inyu bbic cga pehhebmx ex gwu knuzvufay im pyo golben.
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.
Hog aju xkubp nio difen’c qiy buiprm duewfam ej aj jqit rawel Fid ne ucofiyh ans ewuper: ocn vowohmob cxecflugb deyer.
Us wucf, Lad’p djohmwakq pezpuyudn iz lxoq pokf uh uqopc kvop dudf odqup foqfoov suqntiy cdpxoqs, jobxu ur mannx ahwcayikc sabp nabb tfu duf qidh xevesugold ti oluiw wyaif ykuyewcn.
Uw fje naqk hzitkic, qai’mc jiuyw nniw ziug juujzb duatt, fik di gpooda gburhlin, cic Cim “wcudkb” efaaj nxezqhuk ak xeil fodizovowq, zde xaygezemma cinjier dagud uwk jisude xayuhewegoox, fic da zfuvgb qhuyzpoj, pay wi gosizo zvulxher uzq bebu.
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.