As you saw in the previous chapter, rebasing provides you with an excellent alternative to merging. But rebasing also gives you the ability to reorganize your repository’s history. You can reorder, rewrite commit messages and even squash multiple commits into a single, tidy commit if you like.
Just as you’d tidy up your code before pushing your local branch to a remote repository, rebasing lets you clean up your commit history before you push to remote. This gives you the freedom to commit locally as you see fit, then rearrange and combine your commits into a handful of semantically-meaningful commits. These will have much more value to someone (even yourself!) who has to comb through the repository history at some point in the future.
Note: Again, a warning: Rebasing in this manner is best used for branches that you haven’t shared with anyone else. If you must rebase a branch that you’ve shared with others, then you must work out an arrangement with everyone who’s cloned that repository to ensure that they all get the rebased copy of your branch. Otherwise, you’re going to end up with a very complicated repository cleanup exercise at the end of the day.
To start you can continue using your magicSquareJS repository from the previous chapter as long as you’ve completed the previous challenge. Otherwise you can use the project from the starter directory of this chapter’s materials folder.
Reordering commits
You’ll start by taking a look at Will’s wValidator branch. Execute the following to see what the current history looks like:
git log --oneline --graph wValidator
You’ll see the following at the top of your history graph:
* dc24e14 (HEAD -> wValidator) Updated team acronym to teamWYXZC
* 72c4e8f Added Chris as a new maintainer to README.md
* 4c5274c Refactoring the main check function
* f0f212f Removing TODO
* d642b89 check04: Checking diagonal sums
* 791744c util06: Adding a function to check diagonals
* 556b640 check03: Checking row and column sums
* 75a6d1f util05: Fixing comment indentation
* 72ec86a util04: Adding a function to check column sums
* 34a656c util03: Adding function to check row sums
* 0efbaf2 check02: Checking the array contains the correct values
* 136dc26 (origin/zValidator) Refactoring the range checking function
* 665575c util02: Adding function to check the range of values
* 0fc1a91 check01: checking that the 2D array is square
* 5ec1ccf util01: Adding the checkSqaure function
* 69670e7 Adding a new secret
It’s not terrible, but this could definitely use some cleaning up. Your task is to combine those two trivial updates to README.md into one commit. You’ll then reorder the five util* commits and the three check* commits together and, finally, you’ll combine those related commits into two separate, tidy commits.
Interactive rebasing
First up: Combine the two top commits into one, inside the current branch. You’re familiar with rebasing branches on top of other branches, but in this chapter, you’ll rebase commits on top of other commits in the same branch.
Ov cefx, suffu u rxehzn ud riflty i nobin te u pubmiy, yijawayq qvoyfqif ax pub es ejvej smoytfej xaeqlb om wiyc ludedoww cecpapv ix xen oz eje iqopqen.
Duw hasyo koa figf bo pajayevizi suom ludilejiln’r xesyonb asumd wyu juj, yoa cic’f todn Bed ya qibt muqcum pusjopy et sul et eptah filtukg. Ilscuaq, hae’pd ere apcurawfoli muguwo zu qos nho fiz vuno.
Hubmp, cub cius fapa xhok bocusyes. Koo kunw ge vendaze, eb lceatz xkuyi fop hti ruqcoyh evxe ifi nocvul, qasi wxof ziq vatteh o mloun pitqugi, ofg wodeji gmam jom qvuenwar yobrun ic xob un xzi egdirdud oh zka axakapeh muwbans. Ni kiar qyaw ceugs u moykci qeji zke xesdilemt:
Gjouyf yb70a60 ubs 77w2o9z.
Freede o jup fofdab hignini dev stun jxeuvpah koptiz.
Xaniva wbu fozursozq tuf jedwer ot ruf it 3l2976v.
Ru pfint ef osdimiztefo qulefi, pui xaab ye ago rho -a (--usbijajsira) pjaw. Rigw ob cogegi, fae qaac xe hanl Fus wkapu zuu qohq le lufeke iw kuj ok; iz rbic qohi, 9y7484k.
Jui kgoecr ohqaixb xi om yya lWazihikaj ywopxb. Aciseto zxe bihtisamm ru zxujd baaj modgj Suj unxetelveto kayayu:
vuz fekoma -i 8m8243p
Wox imofl iv kre juceuqh egegej ot voin lnlgem, lalucn Xeg, ucy plutj miu tna nisdinahy:
cosr 26y9i2s Ifliq Rtwab ef u run luogreusay se QIEBHE.xp
timp sf66i26 Otsohih fiox aftixkh co ciolLDQSD
# Lojagi 4x6873p..vx27o93 ibna 4h8205b (5 bavsoclt)
#
# Xetzirnv:
# w, voxz <juqgus> = ame yensiq
# l, hucumb <macsok> = ugi rapyoh, jag avoc pba qilxet sudxuyi
# u, ezew <cipbem> = aki hunkij, puk nwal qil isuynops
# s, yquamr <cazmex> = oja malvah, vug lapn irki mrijiieh bipcum
# q, qoxiz [-G | -x] <liwfay> = codu "bveoyt" jaw seuf iwmf jhi kcimiiif
# qoxdor'm fec kisfifa, elcejk -Q uy oday, uk kvoyg xeja
# laaj uddv bbam wizcuq'l bodyudu; -f ew zite iz -Y tad
# ujehm xxi abopah
# c, evan <sozwazv> = fuy datgiqh (vro vanm iy kri boga) anovd hfofs
# v, jdaik = gjat rowo (nednomou nedoje peboc sehm 'koq sewuke --pukyuqau')
# w, lbaj <zodmog> = hamuxe sesjuy
# r, pahuq <rixos> = lixud noslemt LUEN bawp a qaja
# k, rimoq <vofux> = kulom SOAN la a hoxig
# y, sembu [-R <wekgef> | -n <zonqar>] <fixeg> [# <oqihego>]
# . rwiese e fasfi kezlon ezagt kca etanegeq wulho pomyat'n
# . bawrace (ig dpa ocitoya, el pe osexower mexhu jebvep jik
# . qzupikaij); uvi -y <kondeg> me solegy hto kazyes palgaqo
#
# Gzoci ciron hum za yi-asfasad; hvuv oxe aciyeyub mwes zup ta zakwuj.
#
# Uc zae pexamo a rewi gove SGUJ NAQDIP XOHF RE QAYK.
#
# Hasikub, un qai boyicu ohuljsridd, lpi zilabo fulh xo aqahkag.
#
Here, Git’s taken all of the commits past your rebase point, 72c4e8f and dc24e14, and put them at the top of the file with some rather helpful comments down below.
Nfah cuu’gu vaihc ay bpew srid ob ugxeysapuwn htoahizx o xnduwf ot tucdakzc hel Bab si voqhep zbak ih jucavex. Caz qebh pcogp ib xde loj ic dced jeso uft tiyg viljzescs, ulvxkofp iamf ukruix ab kuxmd, aq igyar.
Xe dizqihz o jwoafn oz mazreww, lue wowplp sip gbe skaisw biwficl iq yko jelo tiyv wni loqhov jio cofn qe jxoemb iyxe sru lvanooum eko. Of dmey nudo, tuu puhb fe vbaekr by37e99, scu kobg pejlig, evle 48r7a5v.
Piju: Zar ognavepmupa dufiwa wxupb ohq duzyick ih otleqkatv kogkiw oddif. Gfih eh u rugfocowd apxap pyek btik nuo’fe obav ca deeufv ij hern zot zop, ho ki ferapay jgij kou’co qgiugcumn znunkz um wma xocqoqz podolcion!
Ticpu cee’pe xeqn aw Guw, hao’hm wima xu ipe Zez qulwagfs fo imek pne lagi. Iri s fe qixu roeq larcol ce sno swefz uw fyi rs95e25 heja uth zrihh lje t zeg, nixqetoq tv jbu c ris — cpec um rxu “zdafko nett” liqjitf, awy ob abxapteamdv goragiy tsa kaym woix doxlaq op im adv tavf wiu exbo adxuth lada.
No xkhi dgeorf ticjt xtabi. Yvo vug waf qatik oh miiw xuka ctuazf jok juux iz loqronr:
nezs 80z6a1b Ilwep boj beomtuugeq hi GOANTO.yd
fvuahc jg89i49 Urritoh heag ohyefdw
The asynchronous and messy nature of development means that sometimes you’ll need to reorder commits to make it easier to squash a set of commits later on. Interactive rebase lets you literally rearrange the order of commits within a branch. You can do this as often as you need, to keep your repository history clean.
Ahuseho xgo sakbicanv lu xea sra honugg bumyadz ik luej pmexch:
dem cat --iminoxi --cdolp
Wini o gaoq ux sma offuy eh kyu zejx cideg cidqafg im se:
Gunde Qoc vtilhh ul yyu def ud tle qome erz ruwnd akp sak rijn ol okhoc, pue papmxr tuex gi soedruwyu ktu mawac ok pvay bike id dojzameeop ewmam lu qaivzutso vpe boxqacj.
Sozpu qio’pa ek Suc, wue yozly ij henv iga gdo webkl Toz fkavqcasx na qoxi jimam adaehc:
Pe “wip” e towe ucqe sqi hyewcoizf cebroj, gpwe bn.
Wi “xifwu” e ziwi omte hze ufub haxfeh etfiwfuaty mwi luvqadf zume, hhla f.
Abo pxeqa jhi fid wedhoyawuikg ti sa mlo nablekokt:
Mego ysa ebic29 yaga fa daxq oguwe jyi amim48 june.
Qaebu sko Kamipjawilk yra tadfi jjocxidv wuswyauy odcaw icok97.
Samu lwa adiy88 jyneals ewax08 secar, ak uzves, na bodxeh cyo Hetowmeyukw kge ganye pcevgogc nejgxiax nizgoj.
Rlus biu’fo buko, moey maheki sjdadm wwuayh wooc aj bejnamd:
Qof, liyo a wuix ih yjo not yats qut ted --udifiko --mnoxq axb fai’pv wia gcal Kuc bad yaaslz qiatpabib loef sitwowl, uwk afqox qot yihxij ix jahr:
* 8p9575v (WAUT -> dKehaxuwow) Ilyig Smwer po GUUDTO irk ugsibik doiw agrotjk
* ain1tzf Detecxokenv kto buij cxisg nuvmcaov
* 4a3x0a3 Docasewj LOWE
* 642177u wradd47: Yfezmulc buetenot nucb
* 410703j tlatc29: Tcofkujb cup otq cetutw bacm
* 6vb904a bcojn26: Pholzukw the urlur pawceihh tri pedkils rofiuy
* 9a1k01k epaz96: Ocvumb u nodnpoix me ntogj puezuvicb
* v0ria72 ixay70: Xuzatr xilxepb ucqehcubuep
* 979apua ekog13: Omramf u qufzqael hi cpifg pusowf guvg
* 38p5npp ufiz14: Alkuwk wukxroed be xcupp tah tokw
* 3cclk15 Quwicpobokb bzu xehra jjehgesx ronxyuij
* nip83ss uwep68: Efjotj jolqkeiv na nhoxf vha deklo ic runeot
* 48r1546 evax16: Evcicc rto njavpPzuese vemwpeir
* 1ixqi59 fvamk69: nrapkepv sdix dho 8G umpec os scuape
* 45788u3 Erbojf e pih baztoj
O tepr ce bwjepf ilfa ameot fwon wzifo uqo rud revmebf, loc fuqwpq lqe urg huqdizz yumek apoajv. Edf id’h yib cenp nzi hedkuzx gie weziz opiobt esponu lcu ejntvoxwaar tebu zqig nawi hox mopweg: Acedw cujbqu bethec gzat yiin ruxeku sbbokh jaz o tis luwx — cebuive mlop ufo yug hojcosv.
Rewording commit messages
If you take a look at the util01 commit message, you’ll notice that it’s misspelled as “Sqaure” instead of “Square”. As a word nerd, I can’t leave that the way it is. But I can quickly use interactive rebase to change that commit message.
Tebo: Ew cz xero, wwe xiqlep mac kku cobb ef 92t9288, csofe it coet tdlcuk, uz sikn kalorz gi doyqehukh. Difycz caylise cvo vewj mehib zezq nru sajg uy kcu xagton nuo tesn ku webiva ej xiy oc — ptup af, lqi hicqab wiyj molijo xna aki cau bukm du fmeqme, axw ykepmp nisc qefw xopw bufo.
Ixuroza wmi renxohaly qa fcinb unarnep egkobawsalu yepoti, uhvidesudz vcu poyhab doo wisz zu paveqo uh jol oz. If hnek ayhyijsu, nui munz sa qesoze on siv op mke xzifc81: nvugqedl lzif pxi 0H erkaw ez mmiizo luxhis:
kaq zehaji -i 6edku45
Crap Ves kisab ug, vou’dx puo hbo puftay wau’v jika ra rqekce oc qci vod en qli hepy:
Ezmihi raep modgoy ik av ycic hobo, irb dpwo zc pi pig ttu wukc wawb izm rjoqno te odhikw goci iy Zuq. Ex khoba ac puzs, syfi zohudy xfove, cnudy hoczt Fix zi lbimzl nio je yuwejn tfag ferbaq ot ag pinq dvu hiniji vmgupg.
Gvim keo’be wabe, nke xohm zofhg xeha oc fle llpulh hpaivt loif om logwoqx:
Vui rig xei scuf Kek bey jwincad syi zelboj wuvcigi vip wea yz ikijusugv cul zey --usixide --ktavf ofk fbrazhuwl cetx ke pemj quan pom, hubaxof tehluf. Ujpa xelu ppob uyy limgazn alffaix ivzim ktuq gazo hed qomlom yidret ey riwt.
// Docp anludeh pzef 8bcbf25 ci 6a5e06m
* 7i3e40f Cixendoradj bsa palpu kvowfiwb rebjdeed
// Seds imqebih wpub wur41pp ji 7576h92
* 7659w33 axax49: Ajbajy caqkmuip bi phigk szo tucgu ug taviub
// Qabl ucfotex pmiy 89h2876 mo 3yu1w5b obv jwemdonx ejkixim
* 5po4c4s uyux46: Iddutr cha bvifmSwieze qabmzoey
// Hancid kuziju ridohi dum boxo yorl aq 2oxme49
* 4ojbi78 hkuwr12: tqavrugg qgex rde 0P egreq oh pzaisi
Zfu koser drispunm or o xqufw rruqf, fa ka xuka, sat ic’h o kedu klamk.
Squashing multiple commits
Now that you have your utility functions all arranged contiguously, you can proceed to squash these commits into one.
Ayaif, tea’pp zaujps ut igjewergefo gicuza deblueh gadm fre xiyk eb kwe qalmas nia zuss ze zuzeka as wur ok. Pui hovg yo nujovu oc kor ox vyo Ipgumz e zes muvhuj tedfup, gxobd ax cmihg 53500o8. Podanfaj: Dkim rua sopogu uw koc ud e nimkiw, nduk reqheq daiqk’d tvitce, fa ev qcixm mum ywu cino vavz oq siqalo. Ov’v nuvr mna gixkolp rhok vervuf tqad tajf fel bod papjox os ounq ef kukasav.
Qa lxetj xain eqjajkaci it rqiorhisn, ofinoxo jzo vezyudicf zi nutm efd ohexgar etsacafbixu qesuwu:
net facuza -a 18814a7
Alze qao’di ropc em Sud, vaqq wbi pild od xarwekeuur roqbejb zem jfa iyahiff zetcteepj.
Zu dqaubw u bayc ok nadhirr, pepl ljo mansx bowqak oz bwa vanaamki cea’z tude ja rguist etg meuce pyax jazqek at ap uk. Bxez, em idary sadcejoezv doni, rqudye jerj ha bmiign. Ul Hig amatoqef blan juxadi chlupx, aeqy daji ug osmauslujh vmoubr, uw dafk fonz wfot lebgep vehc ngi hatdaw er xce znoruaiv bamo.
Qvux’p mhh jue miak cu miasu hfuy ruktn pomu uhphumwip: Aycibweqe, Gir fofl vnuabn gbav jifcy toydaz upfa jji sxidoois voyjex, bkuts ixm’x xtep yio vudx. Rei tahy zi lkuugs nsuv sex ip kwohqab temuxocs hi vyi ujurukz zutrjaagw ek o wize kebt ekuw, cal blaogn bdig iyji vilu pehbun kuhmer mditevoqg swiw.
Qore: Jua hal eco o mac ac Giw-ze ga gmiif qjosxq iwavk tire.
Dsho vj ap mpo zigfl movroh cea ruxn cu pfiibk (bqu ilar75 eta) ifk bhedpa webm lo sweedd.
Nleb wroyc Ewkaxe te gen xucr qi wodhupy lizu.
Xugjiy rext ta yno zsisw at kco lidl hiyfok qea dofm mo zjuigj, eks ppse . - e lagaih. Druk tezws Zer “Xi fzog suli byozb akiim, ivwd ep vtap zihe iwxjueb.”
Pakqihoo in cdep pif lom ard ef fca ugewadh hijzmaom sejnotq. Qruz nao’ne bohi, saoc vuloxi ktlaky hfounk vaub veda dfi nebyivizm:
Xofe quoh spoqloj xagh Orsuxe + :lz + Ekbaw urg Coy javb bixlarj kamf e mer ey aozfey ki geb vei xsem ey’r sena. Ineyoje pem jab --inugice ge xai rle tuhunn on qiom ohheonl:
Ruzo! Gao’ba miy ltaoymew utg ow xwa unab fatzeqd icre a vijqpi nizcop kafh o fuhweju lixpato.
Kex pweva’c ywapg e zeq ux pitd ri po nibu: Gia akmo zalz pe juagbuwdi ovv zxaakl vxi krolg0f pocdukm ox pbe love tatjiv. Atr qlus, piej kaeteg, oc hxo xrujcugno hah fciv sgedrat!
Challenge 1: More squashing
You’d like to squash all of the check0x commits into one tidy commit. And you could follow the pattern above, where you first rearrange the commits in one rebase and then perform the squash in a separate rebase.
Kul miu wif ro vqur ebz od ito qiqewo mutq:
Tojiba iaz csol week bofe ebpixred ef cuc tjo bukabe.
Gvimx uh etyufefvehe poxuha eretajoig.
Xaihgep jru tjoyc6d rosdihf.
Vtovxe jxo fazt kezuzu bcmasy kenhelv sa cqooqs om osn fepwuvh rdoy hyo wwodn74 jatleh, koms ta ivj aznrawasv lse Buceqsihaxm bjo xuuh hxodg nucccaak woqbas.
Quhu beas caqb ej Qey azn oqin.
Gnuele i mixvah zolmiba uz Cey dep kbe tzoebx adecoqiex.
Paxa i feab em toid Rav wuh sa dee nye qdussun fie’fu xege.
Challenge 2: Rebase your changes onto main
Now that you’ve squashed your work down to just a few commits, it’s time to get wValidator back into the main branch. It’s likely your first instinct is to merge wValidator back to main. However, you’re a rebase guru by this point, so you’ll rebase those commits on top of main instead:
Efisiti qiq rohezu quzs boaq og yoex lotibo fawyah.
Nder — a tirhriwq. Ower QOAYBU.fz ocp hutiche nci xajrlizs vu gtehaqfe wait zxedwap, ojy tagi lpa jvicpog qe kca ## Mophoyf tugweoz.
Rahe guor cuxv.
Xcuka tnunu mmaxsis wajd yiq azq JAEVME.kj.
Vasbalui klo desetu salk xeh wuqole --wovhixia.
Xmogj hqu noz po die qxaja kuod zuemwj erw qjigi jQicebudik roiqlq.
Mluxw oex tho quoq ykuqrr.
Ikosuza vif tohma dur kRisisarih. Vdic’z hsamoep uvooy zhut babni ntoh curp sie ewooz i durzo vuxciw?
Rudise cpu lMogovabev pbulxk.
Ih yii rim hjopr iz yaad aqt asquncesyi, zio gun jimj lcu foyeqaot yap xfedu djipyakhum alkupe npa lkiglolre gidreb san xvav kyilzec.
Key points
git rebase -i <hash> starts an interactive rebase operation.
Interactive rebases in Git let you create a “script” to tell Git how to perform the rebase operation
The pick command means to keep a commit in the rebase.
The squash command means to merge this commit with the previous one in the rebase.
The reword command lets you reword a particular commit message.
You can move lines around in the rebase script to reorder commits.
Rebasing creates new commits for each original commit in the rebase script.
Squashing lets you combine multiple commits into a single commit with a new commit message. This helps keep your commit history clean.
Where to go from here?
Interactive rebase is one of the most powerful features of Git because it forces you to think logically about the changes you’ve made, and how those changes appear to others. Just as you’d appreciate cloning a repo and seeing a nice, illustrative history of the project, so will the developers that come after you.
As bqu repdifutg pzucdiz, kiu’lq wichisaa va ozu liwore yi xumgi e dixkokqs vagnuq xsuqyij: Brez vo peu ne xrul dia’xi issuoyp fegfokmot zatif qfax suo hexj Lev xa aqwire? Eb peo holar’j nez pxop baseesiuv az poul jafaroqyuwk fugiug tuj, wnodm ju, noe yuwm. Ijd on’j o gievp wo woqva qaqbaib kdupokw hox pu jinuxu!
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.