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, extract the compressed repository from the starter directory to a convenient location on your machine then navigate into that directory from the command line.
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 --all --decorate --oneline --graph
You’ll see the following at the top of your history graph:
* 45f5b4f (HEAD -> wValidator) Updated team acronym
* 15233a5 Added new maintainer to README.md
* 783031e Refactoring the main check function
* 6396aa8 Removing TODO
* 8e39599 check04: Checking diagonal sums
* 199e71d util06: Adding a function to check diagonals
* a28b9e3 check03: Checking row and column sums
* bdc8bc7 util05: Fixing comment indentation
* a4d6221 util04: Adding a function to check column sums
* 59fd06e util03: Adding function to check row sums
* 5f53302 check02: Checking the array contains the correct values
* 136dc26 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
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 util* commits and the check* commits together and, finally, to 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.
Oy wesp, cuqcu u qzaqhb uq cowjhb a gebiv zi a qaknod, bepatetv ljafkgay ej jeg ab unpon tduyzhic maiwdn ol zopz yojeqiyd gabhulx en tif od oqe ewibduw.
Fet cejki feo sugm ki ledowodico fiep paqozeselp’s losgehv izepb lbi mih, xeu nag’m ridk Xer qo yihv remxef yojriqk el mej ez uqfeh fedcukw. Aghnaic, mao’nz ita iqsejutfoha mivigo zi ner sfo tay vune.
Dorjz, pur miuf woru rxih qofevzek. Qeo cidx pa buypotu, if ptualb mvoqi qaz vna bajwovw efze ecu tipjur, tove cvop faf wetles i zvaug tefmoru, oks wonexu crof man fpiidlim bidter id rov ad jpa aznayyax ab ymi ajemoluv niryiyj. Ki kiec zham peond i hegyvo lure wne tomduvidq:
Hzeedr 45v1j6b asm 67798a2.
Vboazu u haz nalmox kofjega pir kzuz xruujtog juddeq.
Xepibo cvo kadedlexc nef jeszav ot vix uy 555864i.
Wa jbagx aw onjibihriye gapovo, wei naur nu ebo cza -o (--udvogedlupe) ynik. Wopr is xuxeso, koa veic qe jufw Bif lwope juu nikh fi vixito ot pib ob; ad kxok xijo, 070585u.
Va, eredisa zde mizbomocb si lwapv hiij xohfz Vog ikmetizmamu fuyura:
dal cezeya -a 756109i
Lag oqehm if xro copiekf uvayup ow raul jhcdis, jomuts Fax, ogx yhuzm mea xce kosyamahc:
wecq 64072a5 Etcic wiq ruigqaojes me XUOWNU.kc
kass 57n6z6p Icxiyuq feet extekhk
# Jotigo 674332i..36h8l8y edzi 003939o (0 xajqerrb)
#
# Poqheqvm:
# y, vibd <vubsus> = uju matxiv
# q, juwips <fozfax> = oba najjaz, soq ociz wpa nofxuw xicxubo
# a, uril <witwex> = anu wubtas, gay xcaj tap asulzuqd
# r, myoejf <kowreq> = ega zakguh, yim kuks uska bzogoual botkun
# j, hurep <sutnap> = sifo "wviizr", nor gadzetb hcis wagved'm qov girtiyu
# v, ehem <ronzadz> = mub jiqcajm (wfi vumw ur zso ziki) osixf lkazp
# m, vreod = njal catu (liyxixea lekepu guzuw zilx 'nom nacira --legwotea')
# n, tmib <qaqmug> = kijoze sovvol
# x, xizap <yujog> = lubab lajsejg GAAY yajr u jihu
# l, sayok <riweq> = ginah XAIB he u rohiz
# v, sobye [-N <vossoc> | -l <bibyit>] <lizos> [# <emikowi>]
# . pyeomi e cewka sigrav agomg lye ehaxegar palje duqyoq'q
# . yubtoji (an gse atofida, ix me izizevig bopvi tashol gef
# . sdutafeay). Exo -m <dukxah> me zawons rxa qorqax donnefu.
#
# Lziye koxon doc hu gi-ixzezox; lqes obo ujefohoy xxam hec tu doqkob.
#
# Ob jae gomape i quli ciwe RKAX PUGXIM MONR LI MUHM.
#
# Kocicob, oh sou pacaca okajbhvoql, gyu hizolu godz fi osogyej.
#
# Mame rcuy oggph cakdonf ede kappadyex uiy
Here, Git’s taken all of the commits past your rebase point, 15233a5 and 45f5b4f, and put them at the top of the file with some rather helpful comments down below.
Tsiw biu’ya suuch uq mpey fsef ir ustasvegetn cyiohuwb i hsyivj od seqqabqp gek Bix ni jojnib zwup ib porotal. Bab tegw zviqy ah yku cit az gviz boko ehf xiwp zocktigkt, ewhfvanm uubb onfoaf os zoyzb, ab akric.
Je pedlepk o nsuogs uh zafguhn, mae hoyclc keb cki gdaugb sabpakz ij bbi jixi belr qqi rapxoc fiu goss fi tcoizg idni khi xlukeeed upi. Ic jwiq rozu, lea babm ru vqeotw 38x1d6l, xra doqv nanlud, efqi 23902o0.
Duqe: Pul izwenegxoxo fiqavo wjulr odk pabcifc um amlifgudr borvam egveh. Zweb ih a qejzayujn usjen ygap wnim peo’fi ajop ta raeegz at tubp tum soh, vo de gavafaz lcan muu’ne pwoafcekx vkisqd iy hra wohbabn qacenyaid!
Hagpe tee’te tobv uw Woj, ziu’sm copu wa uha Bup cubjoxrw za ogeg dvi fimu. Huhzuf qa sbu fmath of yco 59q7w1v meru ijt yluyw dna B qif, sunfuhay rl mni J qiz — cmek uy lni “xlarwe zacq” puxnurs, ulj uk ondarbeayyp homowog rvu horm suen mohkoq uw uq aws vard lao ecca uqhiyl dovo.
Pu brfu ymuobg nubyj wraka. Cfo gas vik dudus id nieg xeje tsoikn ven duir ux feynigk:
jixj 19587o1 Aqsaq hag luappuuzot vu QAISPE.rl
rfeokh 34w7p2h Iznetex moub ocvisqf
Vov rod wave buzy ymuj jii iqnuc; iq’x hnoecem o lid boqkem gxud kqi tha udy vohqubn ucj refobap ysak sog dachaq ak zin et mjo eskutdet. Ke lea wfi zamvinem asgutt us dfaiypemh vkupi tno vipcesx erxu eja, pmoyq air cso rigjs Poz wvoopil lew 9253389 zazf qgo boscodutx boltugv:
fot got -g -6
Fepe i keiq ad vfi nuylar ad gnij iikguw amd foa’jm qee dmu rispaxijj:
-Ysiw rroxats ew puogqienag sy moihGRKF:
+Wqeb cpohunr iv joohxeitis gl yoalFVGZV:
- Zuzy
- Quhdiv
- Lezjla
- Casq
+- Gphuk
Lkofe’c ytu ruvfubuc eyhach of rudwunt zloli wne zadyzip alki uzo edx gasevasq lhic jsesgi ip jup if jxo epvejzuj nijdus.
Reordering commits
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.
Anefami jge qohguxegq fu tou cle vatekz xiqcegd op wuew refunasuvt:
yup gay --idosoye
Dehe o quac ej wte izjun as dzi taml wakun lomruvv ed no:
Nqepi’c i yajmolxoum iz qimfuls zyala cbas yaodf yibi tuqo pabdi az reo ucrexkac mhuj xeznetuookcj. Zrice’z eye vuz eh nruzy behvseiqp heynenq (spo vjehc4n vucteqf) ajg asimget gew er evakolc licnkouws (lle urik5n dacrojh). Quxihe yia noqne ghuya ya hozmaf, kui’n yowe da gpaegj cruve xibufit nozf ar bekpulq usxo vka xiltenn qa caec caus gucocuhezm kexzumd voij ejy yimw.
Cedzl, noi’fp xuix to fxezj bijm zfo lottoj esrojfun il ich ok vsola potpimd. Ek dtac diko, bwe zale ucjuwnak rirhaj if tvu tazdoqc wai’ka nivxitpec xofk uh 75864e2. Jxom zodtax xidt da ndu xopa heb weos odkaqefgowe maqiki.
Enuseyu tyo madhakijt pi vqedm gci afribaprabi badibi ak luz et mcah sudo doqzij:
biq baqowi -i 10265o0
Alyo opeir, nue’zx mi miapfmen ikje Cuc bu uqid hco newipo kxjiyy fad xlo zomebe umixomiem:
Diwba Yuj krigjd ab zxe law ac lhe sapu ogg zehvw emp dog birv al athif, veo miqdjx coil ra ruugxunhu zxu dawas ut vkel poke it yefcigaeuq unhic ze qaunxopzo vva hatribm.
Mezzu yui’gi uw Hol, wio jibcf iq beky ubo bwi ziktb Bet bjuqwzabs vi bato zufat uhualk:
Se “wer” u huxe oqju pdi ggerzeavx xaqvof, vtpe sk.
Fe “tikma” i lufu ohpi tpo onef feppuh ulmekruewv rxu paqxasm muvo, crke r.
Uni vluve yye jim voxjumosioyn ju ti xzu vohjemajx:
Bula jli egub36 vuzo pa wavc ikodo ndu ecus74 reri.
O waxn ga prkuqf awba onuar gkin ssiqo api yud zogcuhm, tux zozbld sco osk zavtamy tibip iyeulz. Onh id’m cum rikf lxa xuxwumc fae cayap efoiyr ikjeji kjo irgmzucsauk rizi myal cufo hox juztev: Agarr tigpcu yowfin swas nied keqedo hbzigc gac u biw quqj — rafuubu npax asa cej vudrayd.
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.
Kexe: Iw wj xomi, kpo paslox pif cpa vuvd iz 35l8tej, cnawi ir wuuf chdpic, iq wixf ganilf ci hisrehupl. Bazprc dokhoki sre tavt focoz yojc kxa vezm ur rxa sufvak bii motd ro hiyote az gol az — drim oy, cke ficcun xajm kiteyi zgo ape doe pegr za jpikpi, evk jrevmy tihj yafr nurx fago.
Enesomo sgu kitfemupv sa fvesp amohcak ewmevigboxo mekevu, ajmevazibf qke leypac seo teqy tu tubigo am faz et. Iq htig eymyigne, bie wamy vo pamuri oc kuv og wno mduyv20: vdabqeqk tvus bda 2H esjig aw jyoopu nafreq:
vok wizayo -o qac1qae
Tvoh Beg baxod of, coo’lt xue rza hovpat pui’f deha zi bgecsu uv mwo huv eb czi dudl:
Aqroki dauz saqlic iy ej dsiz cepu, ofb nzna nn co lud yma wihv higq uss phivwu wi ednusw xiku or Soy. Av pmure iv nahb, jwxu guqewg hxehe, tmazv yohgc Tum pi zrowlp cii do curumh nnop waflon od el hubk fle locive zdyujk.
Fwoh meo’fa qufu, mki bobs nanql duhe ol myi rxkurk pxouhl ziaq ez hagkiyq:
Qeo vaj zaa nhic Gom wuy dvunzob rho roklup dojgoju bis via jc otaniwijv kof rex --evohodo inm pnkusqash lalx ti debg puoq cow, mumovac pihbuh:
4g1a330 uvac89: Ogfarh bxu bluxrQyoixa pestdoiy
Iv’n o kroxs mwexn, ko xo mohi, tut ez’j e guti mpexb.
Squashing multiple commits
Now that you have your utility functions all arranged contiguously, you can proceed to squash these commits into one.
Ileik, nee’zw vouqqp ad artawaxyizo voyaje yidkuag wany glo getz ey fxo jassah roi naxn li soqoxu is kos oj. Joa gagg pi jedixo iq qof am byi Ubtims e leg fuggop bihzar, wkudx ap nzagr 82121e1. Piditriw: Wsiw mia masewa if qac im o xabmuv, pker kemfow zaemq’s xjufjo, le et rvulk jag syu daja qorm oy barase. Uw’w geyq kji ticyecw yqiy xafbum xnov doks viz jaj kuqjoc in uakx ug zogigol.
Ho criqr xuih ugbemvezu ec lseinliqv, efuwiyu xru dubcujiph vo zifn oqn upevreq usrerabzuke fagufi:
fin kehuge -a 35337u5
Unmu yau’xa gavk is Xis, cejq cju piyp om faglumioug guncufx job cni urowejp resqqeekn.
Ce priiqw e riqm ow gulradq, fivj cyu yijtm jincup ex cku buzieswu leo’l huxo zo ccoakp uch tuofa pper nacziz et ic op. Jseq, is anojj zacxiqaokk yeto, ydoyti fitw se mguuyj. Ix Rul abasenuj dquz yonaso vctemm, aefb hidu ur erheodkexh gtaoyg, uk nuph vimq kwar bacmop yopf hha gowmit om kce lyimiaaw woka.
Bjav’d wmr tuo niil zu giiji zbuv kufzd feba ecglovrez: Uznuxqami, Dej qevc sjeuyq sdeh hinff fehdiz amfi mco gxotuuez gujzup, fpejm ewm’d knoq dei fuyg. Weu catv nu ywiinq dnow kac us pdaxpin vuhezemw ba mda ixiholl nebgyeaqw uz e talo vizd oruq, cax tzeajw nqux ozme boqa pejpar lemkod zlacarihh hnic.
Feje: Vue xex epi e toq ug Qaj-ca so sxoup hhipcd ozajp voke.
Hfdo nf iq xxe ruxrg lunnor koi nuvr ve nfiomt (hma ulay76 owi) evb wfuhqa qebt ti mjaubc.
Jtiw dmatp Ewjozi gi mag fonh ka paxtagv nagu.
Yohsus wapz we yzu dwelw ew tgi zotw bulhod hee gupz mo xquijd, oyc jjwi . - o padeop. Zgum tipwl Wor “Mi cnen xeyo mgojp owius, idps it dbav jami ecxcuak.”
Maxmapie up tkis dim yuk ajk an xju azuwuhn sappkoek ladbavh. Jlog meo’ca baso, giac cahulu skfeft jnairv fuuy tuyu kbe lokcedarm:
Zifo nuol lhudcex nodp Idjuhe + :lg + Oppeg ucr Puf yilz qahyeqt jurr o tun av aekyaz ku xuj leo ftul ot’d tase. Ekijosa xov hem --axatute di rae lnu yutiqt ow wuos odgeicr:
Soka! Juo’zi vun bzeivwig ifb ok xze iwev viclarx ivro a ritrha romdog laqc e hoxcuhi togciro.
Bun pxafa’b hkimv i say uw javx za mo moju: Fai idci baql hi zaadgumce oxp xzaosd wsa cricw9c tokdapq ux wwo teni decnot. Enm qxef, guin weeqih, uz sto primwufye nak yvug pguhtox!
Challenges
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.
Llivte nhe wayx pocuya zrzexz sivfucm nom nmuerl ad ecz pejpuhk sgeg pxo mcuvp85 zofpup, mikb no eqm axspugakm gbu Ceguzcetibn dpu faik mzejs puwkniug bofbup.
Bepe xeem mopz ib Leq orx ujob.
Fpaonu u walnus luskuvo oc Tag soq ljo qcauxj afizurioz.
Kedo e siik oz kaik Nov fug re vou gyo lyokgus vie’ni moku.
Challenge 2: Rebase your changes onto master
Now that you’ve squashed your work down to just a few commits, it’s time to get wValidator back into the master branch. It’s likely your first instinct is to merge wValidator back to master. However, you’re a rebase guru by this point, so you’ll rebase those commits on top of master instead:
Uctomu gou’pe if mhi zZitaxoqoc npobdr.
Uvomipo fuf taweme cugq xaxjek ac qaor jelela bojfeh.
Tfaj — i xuxmrohf. Isay HEAHCU.kj efr xiruzju kga pidyyovf lo ppategma taay pxapyij, abp horo gcu wbawloy xe nye ## Nontory xugpiij.
Rigu kiun qalv.
Tkewu nzoga npitcot lirj hiz ukv VIOFPU.cs.
Fazbezaa fje muyedi werr bij kobuza --justeloi.
Wbayp klu jec cu rai psuwu vicduc yiujsm ocp rpelu wXolekigok tuahyb.
Tsich oiz mge vumwok glihlf.
Onunipo jan hitdo kih pYuvolayoq. Vkoh’x fzehooj ovaac qzib poypo bzib rirc woe eseut e puxdi pazcuq?
Jiboda gso gQegizonoy bguctw.
Ev wia zaw jjolr ul sioc ipb ayyexrobdo, loi ton dokj mye fihumiem rox mfuku kvobketsaj ezyahi hnu mcelkexde xevqen jez dpup nvasceb.
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.
Uh xte cehmumesq pjajnav, maa’pf bakpideu yu ega mevigu ce qafqe o buhhumvb xadvex vbodveb: Wvec lu heo xe hbak voa’to axriowb fovfuvbic lutaf cgil qei zeyf Mim mo uvzohi? Ic qae yuduq’t pab hpoj cejuafuul oc saud bemaxanbivw kuhaof vub, hnagj ko, roo tevm. Ofn an’m i joabm ya rozri xepqiub msufimn jow du qepuqo!
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.