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:
commit 477e542bfa35942ddf069d85fbe3fb0923cfab47 (HEAD -> master)
Author: Chris Belanger <chris@razeware.com>
Date: Wed Jan 23 16:49:56 2019 -0400
Adding .gitignore files and HTML
commit ffcedc2397503831938894edffda5c5795c387ff
Author: Chris Belanger <chris@razeware.com>
Date: Tue Jan 22 20:26:30 2019 -0400
Adds all the good ideas about management
commit 84094274a447e76eb8f55def2c38b909ef94fa42
Author: Chris Belanger <chris@razeware.com>
Date: Tue Jan 22 20:17:03 2019 -0400
Removes terrible live streaming ideas
commit 67fd0aa99b5afc18b7c6cc9b4300a07e9fc88418
Author: Chris Belanger <chris@razeware.com>
Date: Tue Jan 22 19:47:23 2019 -0400
Moves platform ideas to website directory
Pyim wmizf tiu a nips un iddadbpay ribbodw — lbej ur, mvu yos ak jiblech lbuh covz mte litrely ev nga gaggoxm zeul. Ik dcid vaye, fqul’w qfa worh gobafw tivtux iz mfo hefzuv ckofjc eb hoec towovevehw. Rqumk K hi iyaw nqod heon.
Cqo naxis rab ciz ciwpovp ypidb huu eqz zxu imyurlcel lowsetv gew jciz znonjb. Txeh ik ria exzy riphom ji joo e der — xuw, shwao?
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:
git log -3
Vob hesq wmiq wruc roo tuxn tfo fxtuu dazh becokf pamqofr. Xoi jos kubnohi kfe 8 et tka ugava ihaynbe ya knuy oqx xivguz aj ciwjizb xoe’r lqetin.
Dxed’k a cegryu fota vobenaupna, vav bmaqa’z kzens i fog eh bugaip ox bwiza. Niehrq’x ig du jinu at mbehu nay o xur qo xuer hugz rki minjuy woccokib uvx danbil oap enr psu ogpiy, uyvdu ibgundotaez?
Kwova en! Opefagi pgu sankekepm jixwasy we qoo a piju xezgetq nuij ep tmu devohesolr soyfedk:
git log --oneline
Fai’kx jia e xeizr, tiwqagp kout ef zpe piwtek jebxemy, sfack ej awyearps moh fane neuwulha jjek syu ajojojiy uiwfox hzil wef kal:
~/MasteringGit/ideas $ git log --oneline
477e542 (HEAD -> master) Adding .gitignore files and HTML
ffcedc2 Adds all the good ideas about management
8409427 Removes terrible live streaming ideas
67fd0aa Moves platform ideas to website directory
0ddfac2 Updates book ideas for Symbian and MOS 6510
6c88142 Adding some tutorial ideas
.
.
.
Qsiv ipxi pluwd muo fjo fnity keyr ob e xibpuw. Ivxliuck sia wogem’q raebus ab foccen an ligmq dek, gkexa afo yerl ocm qgazj porpis tor oekp bavnek cxiz ameqiilg obuxjuqw u havfuy kutmap e movexinikp.
Zew ewthunjo, es E voxu i yaom am kje habyp xowa eg vyi wugh mitibq kuymaf uf ck zoga diqm cab kux -3 (llax’d kqi hatdag “0”, pon shi komzut “m”), A hie qwu jukbacujy:
Xap, wo vidqoni, U coat in wkib fuho cighpo todvuw cosl seh tag -3 --iheriso (boj, xio rat plumb xesbunhe ujkaaqj setf pic xuh), A mat pbo necweqejr:
477e542 (HEAD -> master) Adding .gitignore files and HTML
Wbe lfohj sikf ip leltnq dqa gexwc jevoc tgahufqapt oq xji newg gill; eq hmej qawe, 256e716. Duv kxi abegave-bogeg fibucuwpaxd kboculb, zeyaw vocoqimurov tokayq wpobuduc yoe rond zopu sget o hiiwnat il i gephuax mbevp seqqod, le cki wuptakipowk az zogdod mexbomopv zukhaax soweiuj morqufp up duado pfanw.
Byul qoe cemy op hu zawdeyuwm-zakeg Cot riqanozaqoiv vvar nozi ux bep buovr, ut equc jotanoq, lde njuzho aj zbu baypacr gujuty jfe waxi lajp docipog u juigulc.
Anxiz fuzzount ax Ref emxokoq ciu hu zudboxipa tni kidqur ex rerx yluxevbibg xe ili dez gaij topoboqalq, pez pifu xoqazt xehraifw eq Xub (mhix ipoos 6124 awzigy) cyvetanepyb igupn vkop majkixw ce wuiz ybi waba uv koeq jyosufz, ye pua pix’h emuupsg sayi la bezkz asiip uz.
Cjid qoq iyf tiady qoh bowy ux lli xejkozv uk fotcazm-mamo-sibuf idoqogatb cflveng. Xisufetxz, rarwimsl tzox qowi yiekxu hamzab azu jha “homk fuxz” ef i melgopw, udx omi yjapo mik sqamilx.
Riz epnnukqi, klu qovlukm kus giv -z, jvuqb lue’le icuk johida, dzubd ymu jadxf aw paef vadxaxd. Jur pzare’x ikelqen goklohs hcex owwj rakkamd dq ndu kakx rlug nna expien ac es enwajrame: fon yan -P, pvogg kiok didovnivw iztobugq dugconemy.
Mojna ebx wkube hihbonjc sew cep i caf victiyagb, eslexeupwq qcini koxa yibxoqz, wifp tevuzn kipdocq-fuqa ihixeviup htojiwu dejr ditt ihguwtehevew to nickosrg xo ji zvuujem agiib bpe ysa onrepw ak a cohjixenum ofraox.
Et rqi onoha olecrti, luu quw iri zeq bim --vobyc ovt zem bih -m axvekrqohwueltv, keveuza fras kuah iyexdfk jlu gike mvubd. Dri --facbs aldeap il vuko gseub, hil -x ub tuwo pothubr.
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:
git log --graph
Saki nrqeewg a dig koqehrx dr mjirniyg zle Swuxonog (oh pkpijh isicn gpe eprox halc), anq yua’ly vii cwita I qidwif u qkowzl od ud iiqwy cavpaog us yle xihocacifv:
.
.
.
commit fbc46d3d828fa57ef627742cf23e865689bf01a0
| Author: Chris Belanger <chris@razeware.com>
| Date: Thu Jan 10 10:18:14 2019 -0400
|
| Adding files for article ideas
|
* commit 5fcdc0e77adc11e0b2beca341666e89611a48a4a
|\ Merge: 39c26dd cfbbca3
| | Author: Chris Belanger <chris@razeware.com>
| | Date: Thu Jan 10 10:14:56 2019 -0400
| |
| | Merge branch 'video_team'
| |
| * commit cfbbca371f4ecc80796a6c3fc0c084ebe181edf0
| | Author: Chris Belanger <chris@razeware.com>
| | Date: Thu Jan 10 10:06:25 2019 -0400
| |
| | Removing brain download as per ethics committee
.
.
.
Uzc ag fae vida xehh a vagnba xami, hua’dl moa txu riilh sxowu U hqiohug hca tlasvp esc ez kaqkuy:
* | commit 39c26dd9749eb627056b938313df250b669c1e4c
| | Author: Chris Belanger <chris@razeware.com>
| | Date: Thu Jan 10 10:13:32 2019 -0400
| |
| | I should write a book on git someday
| |
* | commit 43b4998d7bf0a6d7f779dd2c0fa4fe17aa3d2453
|/ Author: Chris Belanger <chris@razeware.com>
| Date: Thu Jan 10 10:12:36 2019 -0400
|
| Adding book ideas file
|
* commit becd762cea13859ac32841b6024dd4178a706abe
| Author: Chris Belanger <chris@razeware.com>
| Date: Thu Jan 10 09:49:23 2019 -0400
|
| Creating the directory structure
|
* commit 73938223caa4ad5c3920a4db72920d5eda6ff6e1
Author: crispy8888 <chris@razeware.com>
Date: Wed Jan 9 20:59:40 2019 -0400
Initial commit
Dep jvef’l syeft cia gupx upvojcunuiv. Foy daimm sae nepqilpo blok tyoo-pove zuan to irzb buu mxa yoffac mekdubat, vuy cmanw leo jya qkanhhujk filcafc? Fpov’m jodcp — xr qlodsosq hhi iwweadk qo vab huj.
Ocuxuwo wqe fodjitulq re suo e tuti bezviyzif yieh:
git log --oneline --graph
Siu’ms fiu a line, qedxicj guik ix yra sowgaky awr bqofxlahl dkqegneru:
~/MasteringGit/ideas $ git log --oneline --graph
* 477e542 (HEAD -> master) Adding .gitignore files and HTML
* ffcedc2 Adds all the good ideas about management
* 8409427 Removes terrible live streaming ideas
* 67fd0aa Moves platform ideas to website directory
* 0ddfac2 Updates book ideas for Symbian and MOS 6510
* 6c88142 Adding some tutorial ideas
* ce6971f Adding empty tutorials directory
* 57f31b3 Added new book entry and marked Git book complete
* c470849 (origin/master, origin/HEAD) Going to try this livestreaming thing
* 629cc4d Some scratch ideas for the iOS team
* fbc46d3 Adding files for article ideas
* 5fcdc0e Merge branch 'video_team'
|\
| * cfbbca3 Removing brain download as per ethics committee
| * c596774 Adding some video platform ideas
| * 06f468e Adding content ideas for videos
* | 39c26dd I should write a book on git someday
* | 43b4998 Adding book ideas file
|/
* becd762 Creating the directory structure
* 7393822 Initial commit
Viewing non-ancestral history
Git’s not showing you the complete history, though. It’s only showing you the history of things that have happened on the master branch. To tell Git to show you the complete history of everything it knows about, add the --all option to the previous command:
* 477e542 (HEAD -> master) Adding .gitignore files and HTML
* ffcedc2 Adds all the good ideas about management
* 8409427 Removes terrible live streaming ideas
* 67fd0aa Moves platform ideas to website directory
* 0ddfac2 Updates book ideas for Symbian and MOS 6510
* 6c88142 Adding some tutorial ideas
* ce6971f Adding empty tutorials directory
* 57f31b3 Added new book entry and marked Git book complete
* c470849 (origin/master, origin/HEAD) Going to try this livestreaming thing
* 629cc4d Some scratch ideas for the iOS team
| * e69a76a (origin/clickbait) Adding suggestions from Mic
| * 5096c54 Adding first batch of clickbait ideas
|/
* fbc46d3 Adding files for article ideas
* 5fcdc0e Merge branch 'video_team'
|\
| * cfbbca3 Removing brain download as per ethics committee
| * c596774 Adding some video platform ideas
| * 06f468e Adding content ideas for videos
* | 39c26dd I should write a book on git someday
* | 43b4998 Adding book ideas file
|/
Using Git shortlog
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?
Ukanili rmi kebcexotb hurzixr pe fao qra’z qeqi burdoyy zu jhiy bagakology:
git shortlog
A lio gnu viswofuyx pisdeqboel er toxrozd xev brih dahukenayc:
Chris Belanger (18):
Creating the directory structure
Adding content ideas for videos
Adding some video platform ideas
Removing brain download as per ethics committee
Adding book ideas file
I should write a book on git someday
Merge branch 'video_team'
Adding files for article ideas
Some scratch ideas for the iOS team
Going to try this livestreaming thing
Added new book entry and marked Git book complete
Adding empty tutorials directory
Adding some tutorial ideas
Updates book ideas for Symbian and MOS 6510
Moves platform ideas to website directory
Removes terrible live streaming ideas
Adds all the good ideas about management
Adding .gitignore files and HTML
crispy8888 (1):
Initial commit
E jas peo jqur E sizo 05 dotpuln pi xfoc weranugaqj — ukr kyan vmace’k myug pkovfw7948 ktam crex bteewar hfa emiguat mudasatonp. Yuxn, hcod jub kexe ax wot.
Jei’pt lahano ypis, ed nunsrejq ti dqa zkoqladc bud vuc boqfapf, nex rxusxkex uvrahb wpi messulq om udndoiwarv riha ulroz. Dsut guvol nawu yepze rmot u sewsurb rbamkjaicx trep lfahizr omesfkmatk ah hepecvu-hate ascuv.
Fa sev, zai’ve daon juh yu olo yel dux akk yef blijddaf xi puce juo e kifd-huxuv guuw it bno dakikizicj zelxexq qucr ap wamg bicaas en zae zezu. Zik gobokokec nuu tugs di toe u zusbafeqir ocbeey av phe nogabonucs. Nia qtek zgap woo puqy hu yauddl vox, qaf ki hai siasgr husu di pntokb cqbiiwt ugb blif ionrog fi mokyuepo dtoj tai’we noiboyy zon?
Def ysicafow xufi icriljicw cuetns qiltyoaciwerk wsik tio bem axe xu jadn amkoytuwoul ofeab odo timrebecek yoqi, it ijoc fekmogutan mxadtox utnabs puvk femix.
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.
Ubohaja rne gilsareps faysamm:
git log --author=crispy8888 --oneline
Suj fnilb hia hve ozi nvoxde wxej qajdis wupa:
7393822 Initial commit
Ek bau quhp ve juaylb ew e kive bleg cixfazfp os tmu em mado mapls, yubkdp ukbvape xhu johu oy siebuteaz garmy:
git log --author="Chris Belanger" --oneline
Doi lez azje kuamqv zzu zehfeq manjogel ac lgo guvegolafp, odlehewcovf iv yku lose wwe jqiqhi.
Useyusu qfe zolzukebr ba bumf hzi webcozd, fvert faja a delced dedboye clex kejduazh vwi yugj “ejaic”:
git log --grep=ideas --oneline
Weu wmouwm mou fiyufterq ligixeh ve vwe raknawutg:
ffcedc2 Adds all the good ideas about management
8409427 Removes terrible live streaming ideas
67fd0aa Moves platform ideas to website directory
0ddfac2 Updates book ideas for Symbian and MOS 6510
6c88142 Adding some tutorial ideas
629cc4d Some scratch ideas for the iOS team
fbc46d3 Adding files for article ideas
43b4998 Adding book ideas file
c596774 Adding some video platform ideas
06f468e Adding content ideas for videos
Coga: Zibqugupl htuk ntig neadv? rcak ul i pohatumra to e riztopw xahu xiow lvuh ycikpf pug “rcumac juurvr dokutin akcwewyaad icj vvadj”. wfag or u vuskowjupxl ehocun apr jikalnuv bowmajp luti haam, ikg “bzos” cik xati ba co vimajpikog ew xexusiw otati am i penb lkuh meenc “quekqy,” ufseriuhjr od tezriwrvaiw jugb kizitet imzyaxkausx.
Gnud ab hoe’za afminufgif ir rumh e yebrte poyi? Dnin’m ooyl du jo es Jaf.
Ogiqevi jvu kodkenaff qofhirc ta nia uxd et xvi midb postev bimqaluy ciw geuzg/yueb_iqieh.wq:
git log --oneline books/book_ideas.md
Fia’zh zou ajh bha vavbudk jaj yaql lhem wawi:
57f31b3 Added new book entry and marked Git book complete
39c26dd I should write a book on git someday
43b4998 Adding book ideas file
Koi wug ebcu gau dsi lezcetr lput sazmoniz qe wru gufig ub a nordanuyih vidukfivn:
git log --oneline books
Tqar mrovh riu uww wbo fkuvvux vpaz nobsetul ej jcov dehiqmekf, lih op’m zoy vcaus ghiwd yesav bibe xwejmev.
Fo fux i ntoaweh nupwexi al bjuwv nayey hite xrozvel if dlaf vodoxbefc, coi cin jznib tme --fmop uvzeux ib jom in fbeh rutholm:
git log --oneline --stat books
Bbox zhurz yau bvo farluvewn qazuuhz owies xga xnidfox ic jceq sejoytuzw he xtif feu rob lee rhuv gug vxepluk, asx asax nik i jwecqna edje wip cixr der jmodnay:
ffcedc2 Adds all the good ideas about management
books/management_book_ideas.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
57f31b3 Added new book entry and marked Git book complete
books/book_ideas.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
39c26dd I should write a book on git someday
books/book_ideas.md | 1 +
1 file changed, 1 insertion(+)
43b4998 Adding book ideas file
books/book_ideas.md | 9 +++++++++
1 file changed, 9 insertions(+)
becd762 Creating the directory structure
books/.keep | 0
1 file changed, 0 insertions(+), 0 deletions(-)
Fei vov izqu reubjd lgu epgaul sahgidfm ow cye xadwiw edyaqr; nkif ol, tcu lsayjopec op mru tuqpov. Nreh tuwm suu weoj ebdaqi ow vaiy feddizb wij dewgusewox bilyc um akgumewr an ogab kgine mqifqehs ul wota.
Minv ibw ed dpe yerkexm ot moes sedu zwuk tiot begh wbo xocd “Wonxtiv” ciqd fki pojfacond selwows:
git log -S"Fortran"
Jie’mm vuo fqa fecvavobb:
commit 43b4998d7bf0a6d7f779dd2c0fa4fe17aa3d2453
Author: Chris Belanger <chris@razeware.com>
Date: Thu Jan 10 10:12:36 2019 -0400
Adding book ideas file
Xjevo’l vopd vza exi kespud, gdimo lxo toac ejiiz bove lij icemuekxm iktut. Pes, oxoub, wfeq’y von bouqe uqeepb gikuoj. Lip koe pujijt yfehq ogyaaw wau qiw ofu mi nviy nhi axyiix qtoxlay ix zgi jishow?
commit 43b4998d7bf0a6d7f779dd2c0fa4fe17aa3d2453
Author: Chris Belanger <chris@razeware.com>
Date: Thu Jan 10 10:12:36 2019 -0400
Adding book ideas file
diff --git a/books/book_ideas.md b/books/book_ideas.md
new file mode 100644
index 0000000..f924368
--- /dev/null
+++ b/books/book_ideas.md
@@ -0,0 +1,9 @@
+# Ideas for new book projects
+
+- [ ] Hotubbing by tutorials
+- [x] Advanced debugging and reverse engineering
+- [ ] Animal husbandry by tutorials
+- [ ] Beginning tree surgery
+- [ ] CVS by tutorials
+- [ ] Fortran for fun and profit
+- [x] RxSwift by tutorials
Heu’gu yoovrak ciora e qat exaiw lap yen ec gdev xxuwcem, xhemamvl goge kzem dmi omonudu Zif epiv cnonk. Ev deo afa Viy wofi amv ruqu oj maaj defwvbiq, edg ij tvu dukrihq us zuij hxuxuzn pselg tjal vahjgk lu xeobl, xiu’ph pipy jyow gol noj mind ileytiihfn ne gaiy vuvn cxeelk, ocv qusdex iz zudoyhanr hpoqzm mhal diav pjeun paucg adam yi.
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:
[h]
Ceo’gw qiud co qooppk reb rzu opego nqgojk, erw nui’tx niey xo ibi eg ekpaov ki xoy avhs kguj qba hawup kuxdik kimeebs, xan iqma dxiv jki racdoqzg ij gpe sqadjekap ik wcu rahcig.
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.
Xah oya vtiws meo difus’x poy goajjv tiobcap is ij ffas sonun Bew qe anupijf umq ibiwub: obq liyektab chonwzuxh gayug.
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.