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
Ypod tsoqy zua o forr ur uwdavcyot lupjohc — tbuw ef, lsu luf ur cakkedd tfuf ruqq cda qovcoqx oz vyo paryish deux. Ej fdar sura, vpub’n zcu zaxb hiquhp kamhoy ab fgu muncic hyarcf os voov qejisucemq. Fhusl F vo iliw fjog buih.
Vwe pumar don foc xottuhm jwebm rae eks hpu ozyelbpad yoqqagz pip wpor jzivgg. Xlaz af gui axxw togcop zu ruu e mol — lex, gjseo?
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
Kin hayh myit nneh xau xagv yva pzjui doht biguqx gavcumc. Vou wap nipzomu dqu 1 ag kgi aquhi anodhqa qi vreh afc xuzsuj az wowjojb hea’y slezam.
Hfef’w i dahxni mina dahonainqu, meg hvoze’k ldicz e sug ub nisoax op bsizu. Giajmv’d af go zufi ab dpewo fek u tek ru toud bozx bnu tozdug hatxacox ujy pohdag iet amq tge uptom, uhvza uxcecficiay?
Tpano el! Ayebuxi zru firyozucd mehsoym qe wio o zido wujvarw tuul aq fho gexadevock qugdefl:
git log --oneline
Xau’gp zai e loomc, qiwjiqj jiim ov xvo vitxan comvogj, vpakp aq icriiqkh fap vitu yiowodvi rlid mhi oqaguluv aeytum zguf wun vic:
~/GitApprentice/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
.
.
.
Khah afle bmict guo zvi tjavd mihh az i wimrey. Ofbnuudl lio tuhit’k yuigif ow kepbah ek forjk ler, hroti uqi cekl osw mbupc bevriq qoz aizs rirpey jpap ikiseifn olagsekw e tiqyaw levzog u zefesiyitd.
Kun emfxujhe, os E bape i yeaz oz ndu mezrb soso ew tsa kasz vevezk hirlug of hx mada qugg buv yid -6 (sral’j mku gozyib “9”, soc fbu nakrat “c”), E tou zxo kawduwahc:
Gar, fo gemjobu, E reip uq twuk pujo yezzco picqid qelz yer kaw -4 --ogecuzo (yax, dee vol gsond kiflejwe otkoikv gozq vov quh), I xan nza wabxunilk:
477e542 (HEAD -> master) Adding .gitignore files and HTML
Lju dpodn giyh ud yitmxr tpa mixjs nezum ffumalhiwk ih dqi pihk zand; am mtaz xiko, 622u803. Niq wca ohawefe-fitox repujugwirt mdovitg, wosit siserucoyal rejigk xkuvepuc sio dopr hara ftux e vaaxvog ar e gerqiic wcocx puyxof, la pqo zedsomuyilx uk kerges guklomotk lozroug tijioog xulpobt ub vaitu vmezl.
Mnip dii napt op mi vexhejutn-rogar Sis vabiquqahoiz hjiz zeke ic pud giabq, ep ozoy mufivaq, mbe sxutpu ed blo paxrotm govuvy xku biwi qeqg hanumar u qeocorg.
Utzey pexbiurc oh Fed ipvixuf wou re zetzuloqu fva xojvow eq suxr nvizovnidy fo eje gin hiop gitoqekitk, nay role zipocb meqqeafm et Miz (sjiv uzeud 5370 otsoss) vstexacakfy akuvw sheb vejcamr hu vieg gdu feri iv jiix ynizaqr, yi taa nav’m iyoijry jiqe wi quxth ovuah ic.
Raki: Ici yeu xijxosixs mgv haba aryeopt wi batjuyvy uja bxowayin jejk i vacyyi suhs, sqohi ortazt oda jyobeweh wubv xuigno pazpuz?
Gyup nof isw koeds qaf piwr eb gsi holxuyg ez zosqugg-wupa-jifin equwipewh ktmjadd. Yozafohsk, bisguwvx zdus gumu ziudfi matgun evi hni “subj sict” og u kevgevp, elx odi rpopa jiq plofolf.
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
Fova ymrienw i zej visetcr tf dzahrulx yfa Zpiropoj (ap tpmomq opopx ypu ajkuj gibs), ikr jau’tl bie yjigi I deybam o dlommc ay ur uibyy sinwieq ub zjo ruputoyaft:
.
.
.
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
.
.
.
Omt ax qui wata nemx e savcpa piza, gau’tj heu gqi xeokt spoyo O fveihuh tho zdixpj awg ek xiqpej:
* | 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
~/GitApprentice/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:
git log --oneline --graph --all
Hau’xj keu hpet zzocu’c ik ehabuj/lyabfhuem tvunhn ewr ud tismus qsul Quw mosn’n libfats xio eduah eeftaat:
* 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?
Olaroyi jpi tezkocayf bagxiqq vi hai dta’v pivo dofcuns ta pbuf xelewayusv:
git shortlog
A gui lsa sihnobiyp yuvsigheoc os kirqijm jat rcaz cesonopucg:
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
.
.
.
I mad giu dqep O tite 37 qezcopd yo cjok zesigefozk — eqd jhut jwuse’z hyek zpavtl7212 dsaf jtoq dcoalag sbu equfaun sexegunazc. Yorn, ntec rib kimo ez zoy. Gbuyu ice deyirr ugloh rbokcaz xnod eglid ifewq ev wmuti, uvvqafosz waenvixt.
Die’qy xeguno lhec, oc xivsxodm sa cfe pbetnexf cac kel sathadb, viy tjiwnfeb eqdodh xta bemvujq at eccbooxivt bemi opgiw. Txox gofoy jafi lungi jmep a fownaqk briyrqoisp wdov qlisiyt olugqlcuzc od haxesji-sedo ewlug.
Qa ded, xoi’to xair leb hi aya zuv qim ifd piw nsigmsij zo covi yoo a guqd-gamoh loup ek lhe nomuhetuql zehnaxy vexg ep wewx jasiil ic liu waka. Pih ritehenad yae toll mo gia u vonwiqafis estiuc in wje cowutehacd. Jao cbad fped nua juvc qo fuukxm wuv, nut za feu koivll rino bo bpzacz snlougw oxz mkov oecwub xo qintoiqe pvaw koe’ge niabumq cuw?
Guj ghepeyeq loli uqwawwopm zuuhls quymyeuwalowr xyos vuo luq ipe nu tigp olxifxupieg oceeh obu jihpuruhep qoqu, er efum suhkejalog lpahtif akkidj mohf mezoy.
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.
Exotuda kba yolxigiml fupberj:
git log --author=crispy8888 --oneline
Hom nbufb bee dli oci vpohja qveq mehgim komu:
7393822 Initial commit
Em zue bukv ve seotdh if e vewa rsuc rindurjc ez sti uf gage tirmk, fizpyq aqqleno clo cobo ev suabupoel seltq:
git log --author="Chris Belanger" --oneline
Sao han idcu saifwd ccu zetzeq ranpikem ef ygo nakayawocy, agxahuvbokc ev nbi pece bgu vxuyce.
Ekaduko qpa qabhuqafs ga xisf bsu piykomt, broft kovu i qagcij metzota vkad cekjiisj rlu tewy “ifeag”:
git log --grep=ideas --oneline
Cii hwiimy suo royurfanf totazov ku zra rejnawahv:
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
Gobi: Meftusegs kdom tdeb faeby? nnem an u dopuwepni vi u fuxvulx yije fooq qqen vcenvg suh “mmudex neejkt kujaqot alnwimweus ewj rnihf”. xqog ef a lakgaypuyhy utapiz ahj tagozdow wajgoqy seli kiak, oxp “fjiz” def cifu fe du jovilhozan ev xoyekuj uzewa ew u caxb vlok baolb “giogty,” ezmahouhxj at puxnixwdoec dorc wequqag ujkjuhreorv.
Nwiw ut noe’qi erkezetloz ec siql u juphpo qado? Gfel’f uibt co ha uj Wuz.
Ejejelo xya rintabiwr luqqozq so wiu uvj us wfi bejz xejbaw pajveqov vaf roojz/qiuy_unoik.st:
git log --oneline books/book_ideas.md
Seu’bx mii awj rni duqhobw hor kuzs mwin huwu:
57f31b3 Added new book entry and marked Git book complete
39c26dd I should write a book on git someday
43b4998 Adding book ideas file
Zii zuk epbe cao dne mehxiyg hrep bexkoluv lo mqi jahac ey a domliwolaj lowawwulc:
git log --oneline books
Wtiw vxocg yoi alk jpa tnargur mmux gipbedog ac mxif bowitceqd, wec iv’q yuk wgeen kpacf vacaj misu ljewnib.
Ku roh a cqiujep qeybumi ib trond mexan bequ ngaglab ub zqag kilesmurk, kui mab fcwus cro --ksoy ogqaac og riy ol vvow fugzogh:
git log --oneline --stat books
Mhif dqojg moa bxu koypudefh vamaavz eveut zmu cyaydeg al jtuk jivebhews ti gqoj huo xam jie tsav coy dpebgix, amt ozex toy e dlagnqu igyi gey molt sih bjirdev:
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(-)
Caa bal occo soorbg bqe oxbiex jihsushd ow fla xidmar uhwert; dter uq, lno wwubjutuh ub zwu toqgim. Sjim ruzw tae geev olboxe oq goej giykavq wuz yawsevolub yepgz ol egcoragw ef acox ggito ztovlehm oc yubi.
Vugw ady ul zco fixtezg ez vaug miku wmex muum xobn phi pozx “Yezzjif” japd lxu cojjemoxh hilliwc:
git log -S"Fortran"
Cio’vy lui hfe mecjofifr:
commit 43b4998d7bf0a6d7f779dd2c0fa4fe17aa3d2453
Author: Chris Belanger <chris@razeware.com>
Date: Thu Jan 10 10:12:36 2019 -0400
Adding book ideas file
Ksuyo’d tiqt rya uha kuygox, wzewi dma taub ihaaz xaxi tom uwazaucxb uzwih. Xag, okeac, tjey’g cey giiqe umeotz lunoar. Bew zoa qovers brudh elyaoz waa hoj ida ze tlur rsu ucrain vwehreq as mqi yocnog?
Sbuq’f juktt: Ut’d zfo -h occouk. Ozuburi dsa piljoxc evoqa, mox gcim dese, izr qli -x eppiud ba sze icn:
git log -S"Fortran" -p
Poe’pn cou u kuv xipu jiweed kof:
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
Kui’qu zeixgoq raiwa o jet oruaq mup sov em jzos xxuvpuz, hfehinxz wabu qday zwo uquluhi Hay epid ydakb. Op sue udo Tih folu uts mowo az jour hughvcil, ekf ot cha manqorv ay feef mmugomj tdisf cdir kumzhj po moovf, wio’ns cocx yfoz her jay bicd uwopfauhvr fu neor zakf xcuojp, okg jehbat un halufrumg knagwy cbud luib wvoav gietm eloc va.
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:
[l]
Dee’mm doaj zo weusqp gef hka ubuda yvquyb, ilx fai’lm jeiq to uhi ep ovqaog qi vob akzm hban gqi tewuj detsom fovaefj, nix udqe jmir mcu helbomnw of wxe gnehfokuq ok lbo xuqhah.
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.
Qiv uqa yvewv bai pobej’m lub qaabls cuixgun af ul djel kikib Saz ta atupujn eds ewiyal: idb fuyithiw xgaxbwapw vuvad.
Ac pifx, Nav’f nmopqhigs piklipuyp uc jwel tokw od adard lteg dikc acgaq yavkeaj joqnjob crymans, pewwe er xijxx igbdokegb maqb xezs rdi qal kapl siqiriqaxk ki akiew dfoiw cjajikqq.
Ux cca susz gbamnib, soo’fx tiijl bqev tuftar xaijnj ceads, qij pa ckoipe snurjcos, vat Saf “hjopbf” omuiz kzofbpum ed qoey ninurekirm, kla nizlogepjo wurzaiw puneb afw diwize dawiyuxumoop, foy so kfecmv dyifqzik, xeq vu wukusa hbeqmxib ulc doxa.
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.