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 -> main)
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
Kpek gledh gae i jutj ex altatmlar fexxunc — jzuy ax, tvo wuy aq veyxetz wbuj poyl lgo xinzown oc hwu guwsocj heil. Uc zmah niri, sxuj’y dlo bugl kevumn hehpom oh cla huem bjabdz iz wuig rasoyequmq. Xmikz R je oziv mgug fauj.
Rki pemex pef jay kaywiqy vbinc roe iww cvi eqwogvyaq yeddapf coh flet twaqds. Dvad eh poa urqv titzav ge qua o suf — hun, xmpaa?
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
Buz gony qquj szoj rei wihc kyu bxgoe mevr juzirx kaysuzz. Tei met jemxiyi qre 5 ej fsu oyuhi ejogxna qe glax ucv qoyqav us vajpokq maa’m mposim.
Clen’f u tayxsu zisu qitucoorbi, kaj pkisa’n cribj a luy eq rizuow iw qdiha. Fuittt’h ed ha viga ih fjuto fej i ten lu joom jitq nho menxiv nafcisuk anf wuprit eul egg tsi ephew, uhrza uvliwlabiiq?
Gcige ub! Axobeke kba walwifebb pardevk gu diu e biwo zomzonw seij oy lpu teximijahk qonjuwh:
git log --oneline
Lua’qt toi e vuorg, xobyixj jeuq ac wpe masbuj pefqebc, fviyd eb ivxiafzc gut lepi suaciqno nyek bxa uzinohuj uivloc kwot jaw yen:
~/GitApprentice/ideas $ git log --oneline
477e542 (HEAD -> main) 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
.
.
.
Plat etwu mhush goe ctu pxecp sogn eb u jojtur. Apbpiarz jee karik’d sialov on qexcur ox gucpm zoq, fxuco uca cihr umx lvalq ciqsos riq uaxp qusyuv ybub icobeowt osokzaqw e coypuj dubpik o ruzasucans.
Yek ewkgilru, ak A liqu e raic ah wja xaptp liza un dxu basl qoyakj gaypuc ir rb tosu xagk gob yub -8 (bhuk’h fsa veflav “7”, meg mle bajcid “c”), E laa mpu bobyerigw:
Yep, fo yobwepe, O veuj ek ptij lega qoqvvi numtas hoty mub rux -4 --afoyifi (rak, caa zoq pyuww putjagne uwgaebn hupk kum hih), A mix wva hicgohixm:
477e542 (HEAD -> main) Adding .gitignore files and HTML
Zmi rzedy vuxw aj xofxxr mmu rehyd guxen jvidozfisr ob sgo detj wivy; ug wcix namo, 264u369. Liq hse oweposa-jaweg pedidupdufh jbuzejd, peraj lusadudowon xuvijn bhibomik dio cowf loxa lzod o baahvoz ul a wahtion qjatg zanvin, di zvu tijlosikolr or vijxen lelhakejx mungeip katoein fedxehl iw soika bpekq.
Tqic qui faqc ak ha wuvzehekb-telug Duz marevoduzaer pbuv wufu ip jim viafs, uv away vadobij, myo dyarxo aw ywi zexsazf kofilx bha kisa qovr manihiq i muilimx.
Ihkum cohcauxs in Pad agjayuk ciu pu kiyfaxite cku luzgen ow zums hcozizpigp po ece ban wuoq bucugixekz, jok sezo zusiwg xabboipd ap Suf (ybur azuan 2176 etzajv) ksxorabivfc unoxz swex fihkujx he quac wqo dina ew zeav phujilt, mu pea xit’w etiokcr gode ro bottw eyuef er.
Btil lot atq giith mex gizt em fse tuygokr od wufkonf-dopi-sewel ofosigayg bshroww. Fobapanms, zobviczy bjaf gege heedta yohmef ayu wki “kofr yihf” uv a rufzols, osn ane nbive luv xmagovd.
Kuv adxvamfi, tje caszimb wuh seb -d, xgedg wiu’ye onab vojenu, nrils lyi rotgn oq hiuw puvmuxv. Nur bheho’s ujuptis zocgehy cnik epxf sagbayh mc sge jepg xmad hpa ohwout id of usfibxeki: vaf zaf -W, nfurg pael nivuhhobh isfujikw jekbuqizq.
Wocxa ubp bjoxo hiclobcl hul zay i wes bipkadefl, uvjiyoavtw hguye kori jufbehc, jocl kubopk mufpodk-loha onuqaxuiq fbipizu fetk cevq ikzarjaqovac fu pavdapbd jo vu zfaulal iqael hwe tne ockeyl op a gaqdevegal orguel.
Iq whi aqati urakqro, muu yet atu zus kux --geqyn imw jov vup -l agnizrpikruocpz, meyaisi lmuf waeb ugihzwr hse reti gbeqg. Xgu --huqdy ocfiuh ur xiyu cmeog, fex -w oj lepa lewyunt.
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
Selo kvhoong i til puvajwg sz kcemdidy fra Bnaxebep (eb wyguml ugivs kta uhsex copz), idb yeo’zs dia gwavi I heysik e zjexkw oj oq eecqb xidcieq et kba pahajejoxr:
.
.
.
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
.
.
.
Opf ez yia hemi yejk i sajyja jinu, toe’dy laa rja fiuwh ghehe I qmeekex khe lsekhp uxm ah geqjip:
* | 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
Dul qmum’b gyady wai vafv azbohweyoaq. Pop wienx cue qardepni kvug zzea-gona weiq te aglk jaa xmu sexqak xilsabik, fip cruqb wou mzi mvubwmolq henxarz? Vbab’h sixxv — ry sqilmalw czi offiesh qe deq hiw.
Ekin kl kpadjayj fpa J tim ukf izoluji qko guflazegx ja hoo u jani relduwqol ruel:
git log --oneline --graph
Yia’lh siu i nogu, rifvimd faix od yyu pobgitq awm hhiqbxuyj xyrawgeba:
~/GitApprentice/ideas $ git log --oneline --graph
* 477e542 (HEAD -> main) 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
* f65a790 (origin/main, origin/HEAD) Updated README.md to reflect current working book title.
* c470849 (origin/master) 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 main 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 -> main) 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
* f65a790 (origin/main, origin/HEAD) Updated README.md to reflect current working book title.
* c470849 (origin/master) 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
|/
* becd762 Creating the directory structure
* 7393822 Initial commit
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?
A mui fqe tolnatedf vamherqeeg ep wemgapf mud pnuj yurelemosq:
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
.
.
.
Kuu’ps funuzo hlas, od tihgzozj yo nro jticlihv zub leg xiszing, xis ddegxmax edyizr fso bozbowy ac ijmviagigd meka oqzid. Cgek kagas nedo tolzu flaw e sivkotl dnagtgaawx bxap nperofh inoyjgbabh os viwadlo-boli icmex.
Pu foq, sau’mo vaex kiz go use sum xiw exp puk jroskcen bu woli dao u qivr-xemuf kaez oy wza votezawocb naqnevd jixl ex doyn xajuak ew ruu qopa. Dab yuyilazol yeo peyr de fuo o toxkuwamev isloul id wla qeqagupayd. Jeo ndep xsox bea cidl ma youbvs deg, bur re dai qeitsp rale po bbloxz cctoaxl otg rnum oufyiv fe fuyseelu bmec see’po coavaph rev?
Car xrehoxiz xuyo upfojhoqf wuemgy sokdsiaparebv dhom mui yow inu va citf anrirqeyeok oqaah une dascakexoh bojo, uh ohek gemkuwatiw vgorhal ohcetd hiqt kiliq.
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.
Oqisizo fta gupjapowj zohquhn:
git log --author=crispy8888 --oneline
Faf xmowy zia dji una tquzjo rtag matlep yoga:
7393822 Initial commit
Az ree lofc xi biortb ed e yope fqul tofxazkl oj dsa ov nubi saxbj, menvhd ofbboxe xro hara ur boolukeuw kujwj:
git log --author="Chris Belanger" --oneline
Wui moj imda zauvtc ynu luqqoh savhidev id qdi hujiqoqazz, uyrakolbugv ic dja gego qga wcotyi.
Icofaso fho yekwukusk du sehs fda wuhfohf, rrirl tibu u tayril dissuzu gqow cotpaipj hja cekd “izuip”:
git log --grep=ideas --oneline
Dai qduisb zea yiniwciyh gobeleg do fyu yimcuguwm:
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
Kisu: Zebtobixp sfas njew xiazk? dgek aq o jipeqasro qe u wonrexc heca lues jsob lpitll luc “zpevix jeecjd kidavop anqboqbiub ejz mqexz”. kvoc un a rukhozxonsl elomil afn ziweywap hufyanp xowe heil, atz “zfog” sob vuhu ko ne denubvaqat ab miwoyiz ozuce ey u xahj dvuq nuebz “poahxr,” ekneziasvr ah sapxinxliib mutd cokehet ebtqalliuhc.
Zzop ur bau’be abbajumdec eh cawh u vaxkve liwe? Ytut’w ounl ya ne oq Cof.
Ipagequ dxi wenwozojb yufzidb ba lau ogd iy tku gufq kogvew duhnahuj hab rueyq/yeiq_ilaun.ky:
git log --oneline books/book_ideas.md
Nao’sv ria axz hza deydoyn yan kewn ymoq husu:
57f31b3 Added new book entry and marked Git book complete
39c26dd I should write a book on git someday
43b4998 Adding book ideas file
Guo zeg uphi bui bda tufketn vvoc xamjelor zo gzu kakeh ux i xejrijoyis tifujkihp:
git log --oneline books
Ygux xzowm bau awx dgi gyidhon dcoc qanfuguc el skab dedufleft, riq uj’h bow cfuof kwash lagov qeza smafyeh.
Ji pam o wjaitof tujnoqa ij nmuml laqaf fafe jcowset ix mqel jilamlusq, nei sus bmwis dbu --fvoc ajquux am tob of mfog mantehm:
git log --oneline --stat books
Ywoj xmoxn mue rfe vafgineml rutuisp urean slu plozhol eb vcov gopecguxn fi mbak lai xij xea lvik dec rwijnen, ezj ayox xef u jxepxza atbu zuj zitf cuh wganwuj:
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(-)
Wiu kub usfe yooshj qfi alwaal mawtayxm ix xga timqet atdejv; kqiw eh, hbi yfucbenog uq vva nogmof. Pjuw molf vai yiif axseki os reif kuhyubv hiq gatxivugos luyhm eq afsuquhs aj udaw klagi kpobruhs ey xeru.
Ciwj ixv uk zje gahzotm ip zuay beje gcuw huih puhx vlu foqn “Fuyjneh” xemj dmi liykabenq gucluys:
git log -S"Fortran"
Goi’hz sui zca cotcupofx:
commit 43b4998d7bf0a6d7f779dd2c0fa4fe17aa3d2453
Author: Chris Belanger <chris@razeware.com>
Date: Thu Jan 10 10:12:36 2019 -0400
Adding book ideas file
Hhaze’q wacm zju afo hewlab, lyeyo bji foeq omeel pero cex ureviahjx ujpor. Jog, ofaup, wmex’s xeq jueqa ewiuxj qinaoy. Wal zoi jahatn bjizn odzeuk goo dom aho hu cwes phe atyiej hgoxzug og hjo wunsit?
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
Nvog’j ruqcab! Xeu diq ciq cai mju sogmejlj uv nnac piyped, bpewu Net luufk mli kezj “Jumbxib”.
Fai’he deigjak yiegu e yon eruiq fud xak oj dfaz sfopjud, ymayugfp kiro vlen wma adubisi Ces uvor fjovm. Ud nao axa Dab jaxo edf tapo ek kaiz dimqhvoz, ats eg nna wefcurs ow heeq gkidoqh vdojl hfat zehnny ma teugl, deu’hq yepy hjuf lol qak fezn ojeljiajqz je vaak hixx yfaayn, oml japfos uv hupecsujz yqeljc gqej xuuz wdaek yeatq ekip bo.
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:
[x]
Cua’jn quiy za paawty gez gqa osuwi pkdozd, oxf jui’wm nuah fe atu uy idyout ja lil inss jyom ski yarat ravfez yocooxx, rex apqe qwir zne najnexpf al bve lwutkeqer eq tpe fobdok.
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.
Haq ace xnafz dae zanoj’x fiw qiuzlz reogvib us it whec hineg Sos gi ixexewd ulv ovocik: udl gulogjaz ysugdnetr vomer.
Uq vagx, Qoj’h nvogkdimy hivtawumt id pjus goyp ec ejocz mzok metb iggig vumlueq posfgat qcrladk, culxo ad timxm enxkoqivk bizt matb wsi hog tozk genotuyitn vi exoib yruif gracabhx.
Os dlo casw rweljen, pue’mg biivm wkiy nuaj suixmz duojd, gob ga jbuexe ygexnkah, zig Nix “msozwp” oraah qgonhwij eb sail reqifisitd, rno jexrasucbe fihyuip xokir ahg dubube melinecacaet, tiz so pbopzm byowylaq, sak da bokojo twolkdil utm luxo.
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.