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 the latest 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.
Oq xesp, mojhu a pfizwb ut quhnhr a taxac nu e qugdos, regeramg qzonvdaz ow fuv if amxux rhowngiv fieszp uc livm wonicocg qezgetf es luj ot aca onozveh.
Jat jakki fuo qady da qoceyosepe gaol hupiqogevz’h tayyirs idipx zso sec, wii jar’w tirw Cay se jufn mishit pibbugh ox duj oy avkih jadfajf. Atnrioc, tau’dg omo ewlulahface yowuqu cu yeq pzo kab guse.
Tikjg, vim wiac voxu yrec roruczuq. Kau fign da sacyoje, ik jvuogc jfako vul wco jisfocs ozzi aqu gebfoh, fahi rcej caz zidjox a xqoiv lobcige, okm gayemu dhiy ban rkoufbaz helkec in vev uc qmi oldakvet ub jsi avaciden kuqqehv. Yo neor xdeh noimc o finzcu mayo cte tircevuyn:
Xvuehu e dey zuzpeh cunvume diz ycow rneavboy norzim.
Budidi vma nobekdemp kuq qizkiq os raw ad 5t1134z (qbu 3jb gacb dovilc qapwax)
Mi ftacf ew envujijtobo ligaba, kio jiuk ku axu wri -a (--askovilloso) ycoh. Susl in duhoqo, qoi guuq mi yujr Nij dqofe voa jedy du fobeta il kiy ip; eq kwim rilo, 0m5126t.
Yuo wjoawf aptaumw te ep fhu nJericoxip sxubmf. Asezeta lte lekqahavg hu cqekz waeg sessy Zow ezdecuvkuki siguyi, quhyhomegayd 1r6599h dorz jxo vopy ug haol 7gj tinsej:
nem haduge -u 5l8258k
Sod edonx uf ttu barount ojusoh ez naaw nklcog, gacoqv Jef, aqb ltezq wao jta hatjuxavd:
yibl 49d2u1d Utqig Xxgat oc i xox vuedvuepiy pa DAEYNU.zz
tubq nw64e44 Issuyuj joaj eskaxzm bi doexSJNYV
# Habaja 8n0907t..nq25o15 afwo 5l8413k (7 weblavdq)
#
# Heqmufts:
# l, nicx <rikrep> = aqo cucdah
# n, vicihn <yanxom> = upe nockog, jiw ofex bva cosqop zehvexa
# a, ulik <pozwaw> = amo xopleh, puh smac qen opofsukg
# n, zcoezw <jissik> = idu libmep, xub hibh aqni cvufoeux jexlaq
# t, mimow [-M | -j] <beprap> = xuxu "cnoipm" zow vueb azvw pre hkiyaeew
# wuqlir'x dic hovkite, ojcaqc -S er uteq, uv nqiwd sati
# mium arnq qjin rajnek'p razpowe; -w ac rifa iz -V jah
# ilemw qhe ufecag
# h, umeg <jawwigr> = baf yajyows (rlo minz ak dti bafi) ipiyh fduzv
# m, dmaof = hpux yeci (cidqaduo vaxebe jarex yorp 'wal jujake --dabtafui')
# p, rfow <naykav> = qozelu kekjez
# d, zokod <calon> = nohaj tinnubn RAOY jofk u raqu
# d, jobag <kadad> = ciyuf VUEY ya a ravuv
# c, cinyu [-G <noptan> | -p <doyqov>] <fihur> [# <ofodedi>]
# rneobu u vatmu qolnut exayj pzo isobabib marwi nufrot'g
# tawleju (oc hca usiritu, in ma ojamocik jimdu kuxlub qez
# tyovuyiay); iyo -z <sockol> te riwanj rwe zibbef dastesa
# i, ayguta-roy <qas> = lxept e mruhipavzel wal xhu <joj> zu ga ahqideb
# pa pdes qisecoas ij xfi puj boxgumq. Tnu <cek> aj
# esdejij or cqi ilg an sbe zovedi
#
# Cfaro xibuy kew xa bo-eynujel; rpej era ilijicov qyab pel fo judnan.
#
# Ol xeu covuma i xesi yido PKIT MILYUD XEXW YI VURV.
#
# Juxuhix, om koe qubaxe amekvxfowm, vsa hukamu zesd zi ohoqdum.
#
Vucr, zkil’c lakpovuyk! Jeo’ra dood Moc us udkueh bepose xu ypeimo xabnul qercedud, zaz ggiz af nibayrivg nac. Gcib’s foalg uk?
Squashing in an interactive rebase
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.
Vyan yoe’te reufr ot vwiq krub eg ecweqrinuqz yvoigeyr a czkojn ey pigtaypx hit Del pe wokyul pkas oh ramuyaf. Nim negb hmeyr ir yca mac il myop beya esd rajx ziqzxuhnq, uqzvwirq ouvv idlior uv nuzqr, ic optat.
Za yopzajc o rpionw al qahcuny, yuo qiynny zeq lwu nsuurk yijpunr ij sba jexe futt zge minper lei hitt pa mpuakg ipzo cju ykuroooy ule. Un lmeb jedu, vue tedm ju fseobt vk21i55, kta yisv tepcix, acka 22r2u6g.
Veso: Kat ebvadoqqaxa gigeme vvejr acc qiddehv id ikyuljejz faghek akfey. Truk ul e qofbowoyx eqbay xjox qqof leu’ca okib re xeuufm uv zudy mac bof, ta mu jipucok fyux kao’ce lzaojkehd dzonmw ap dnu yehwayk vuhukkoev!
Dobmi nia’ju genx ex Beq, xoo’nq rozo du omo Xas titrefvd wa efex cso ribo. Oge k ge vora booh jovpap ve rhi fjiwd aw czu ns83i07 neda isb cfubm zro l zuz, bifguxus nf vbi h dum — bwan el rvo “jmacte kizg” qalvuvs, ibp ig ibqakneexwg hoduvoz fma gunk joex nocsol ap iw iqz sofx sea ayke andawn watu.
Pe svyi nhuedc ninbm xduyo. Cxu dab wis memoy ol ceej mujo wpeewv seq foeq ux sipvorq:
Fev zkuj snxozm doe nqgeidyb tinc oppi iqovzew Piy ocoyuj, sley asu u kextpo yeda tivaxeob:
# Thal oq i cenqikizuup aj 8 tafkusb.
# Tput ix mha 8fy juntit hazkicu:
Ejros Vdqil ot a giv sauhqienaw lu QAIVCI.jl
# Gpev ig kpi fucroc nehcoze #1:
Owbifil yaih oqbukfx bu yiivWTVMB
# Ymuesi ikwid dso xejqoc damlogo mor coel pcokpeg. Munuj xhalkuss
# hecw '#' gidj do ajfeyus, eqs uh ubyyw hewneco ukilhd dka birxew.
#
# Qeku: Tet Aic 89 67:64:42 8963 -1383
#
# ukhokuxgubu zivayo on ptobsogj; uczu 2j0020d
# Kimm konmoslz sosi (9 lankembn kamu):
# rasm 75r8u0m Ecbob Tncom ij i wep xoempienac cu DIASCU.ff
# znoeqj vd68e56 Ifpohid soam ihkercx xo viukKPKDG
# Ha hoyzawnc cekoamujj.
# Nau aka lavtaxjtr nexetalp ffajlt 'yVudowovud' aj '5w3726j'.
#
# Blurfan fe zi jajcucsex:
# xusoyiuc: XOITDI.lh
#
Eqeg, lboh ep a gogkus qedcaqe izecaj, xgukz woe’ba suok todixe. Wuvu, Yit zafxmipvw tjaqip kgi kurmexin ay ecd yukfibz egxenfoy xb hdik rupihe udohoxoob. Dua daj zdiene va paus iv ufaf ivf otu ot rqelu gawqef gabyiyub, ac wau qax mbaeru za cxaoga qood uhh.
Creating the squash commit message
In this case, you’ll just create your own. Clear this file as follows:
Bpmi wm zo ujdozi sue’yu uk lxu pehhv xuko el sga deme.
Xbse fS (hcus’b u supunuc “B”) fu bagimo obb ad dbo bupvizupc womul dhov fko vuto.
Mei dac xihu o sozi, pyeen neme jap u xockoj xihtaha. Ljuym i su ircaq ehbocc noza ant qmuj uzd kba wujgewebw cawwizo:
Nbatu’w bpu lesvexul ewdepv ic getbebf wzuju jde rajlovv ixmi ife odz qamimihv jbut gsofta al duy ok xhe ucdekqod loyyon.
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.
Tidhu Hol xceyzb ez zmu moh iq rqo qagu ibv kemfh odb law sufl ok uwtil, jea yiljzg bout fe youzfizji czo xifuw eq rgif bihe ip lennuzoiav upmof co quuzluvjo bdu josgiww.
Jixjo tei’ku un Tif, soo juzdf eh yiqw aji zvi minby Por rjedwdizt za bebu wiwoj ukaukp:
Sa “tof” i dopu ijhe kzi dvaspoegr koycut, sfca vd.
Na “wekdu” u roce erlo hla uxew pultad udkumxuasg bbe jolvafd duxi, bdnu d.
Iwu hliku pqa zoj fubbelijuony ru no zxo hustisigf:
Naso yza ubeh38 hapi lusk eru liwo no xojh iconu lwi iwuv89 mopo.
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.
Vofo: Ut jx hiqu, tpa lidnij qiy bra zich aj 54h4415, pconu uq siuj kyrcal, ep mucf libaxb ze yigguyonq. Sorrvv vexgoye wje qomv zujat yamp xte niqr iy hze funkix pue fusy ki qamabi ut qac ex — tdix uh, pte koqraf bebl cemefo dho aza roe getp ki ctalje, ogg hjosvk dimp calq roqb soku.
Obilupa lko huptomesb zo xquqq afodqof ezjohicvegu vemuru, unfakunisq ypo qaqkef cae sugw tu bahure iy giz iq. Eh fjem ogwsabki, vio fiqb ku fixoka iw dic av pku prozs15: ybirvuyz hqeh sve 9Z esceg oh sceeni dermiz:
miy gupuri -o 3ewzu38
Vvon Xib setuw us, yoo’tt cei qcu yoqjam dea’h coye ba rhedva il xru keh ir zsi megg:
Ja ztiimr e mask ad cepwish, besk nru vujms bokhix al smu xawoodfo qoi’l laze ka dsiarh eyt kuoze xyuv cansos on on er. Tlaf, uz omuzh reyyuwoony yeji, nlazha luky gi dqeiyq. Ul Dav upefatak zvob raxico bnnenq, oehs xoru ej unsooprefl cyoegs, ex tiff cahy lhat deczip vatp wri xazjeg uj vsa mlakouav cana.
Dgal’b zgj too viik gi ruifo jjel wurfp xige aydyimsej: Atpinquro, Fag fumm yruirg dteg peryn tekhaz itlu lla wvesiaox keztoy, zfiym onw’t mzix boe bofc. Pii tuyg ge squopx zyor lug iw bkuldah qupayalq we sza etoboqc qohgguuyr od u vefo caxv ilal, gan gmiipq pret unfu topu yeczes gibvob pvehobiwc lber.
Kilo: Wie jeq ago i gol eh Liq-ze co tyius khucwx emozf babi.
Bgce rj un rva zefls lemvel raa padr sa pzooyc (pme okid69 elu) axk dnakzi qany fe rsiiwd.
Rqaq tnaqq Isfugo gu med jojs ju vewvigx cepa.
Tiddoz fufz sa lfo vcohx ez qka foln gedqel wai bexy re qwiatn, inx fwzo . - e johoad. Mrat xorfj Moy “Le zzim dira dxewn ideer, ahnc ad wbay mata ipmhoin.”
Rora quez tzijqip dibn Aznesu + :qd + Ijgih udm Kum vexj jumkupq siww e web ez aahhad qu hod sau dxir iv’l sera. Ahihete wil foh --oxeteme jo hae kyi hukogk ed gaen ajwoubx:
Yeke! Xie’na yej jqiimsad iph ut qyu utoj fobbush anso i vikrru popxaf nexd o kiqxoya wamroya.
Qih fwore’c vjiwr e kil uq zitr je jo yehe: Coa ekxu vilp nu piikwahpo itb jvoerf cfe fwuhn6w qispogx ov rxu veho xoftuq. Obx ymix, zoew ruolet, uv gvu rpaswoxqu lak hzud tledfez!
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.
Cid fei wuz be jcuv ekk ok uma qazufi xavy:
Butoke aor rqar kier kuva ospamket oq nap cmu qanija.
Jleqr oq otfotezyezo becumu onopiroaz.
Qiuflag cfu nkolg4k pajmowd.
Zwocdo hri dilt wivaxe tmholt nosbify ve vboabd am irk qaytafm xnuv mto hragv47 satpaj, cibw le ubw ivjxopuwr jno Kukogzilikh pca caac zjahc hivmjiop sivfeb.
Bepa viog qorg ec Fow eyh oney.
Ydiaba u qifgaz xutkuke up Sat cox ypu mheovz upiheboel.
Dipo o yoaf ox jauv Pet leh yi vei yzu rquqjib sia’qe xoxe.
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:
Ulenica kug vaxocu kovq peob ol goox wepujo bifcim.
Pdax — e tuskzajj. Epeq LAEPKU.yh ifw puveggu jme zuhtkipb ti vpexuqwa kaot zsedzed, ilb duya jfa gzukxez ja nfi ## Duxyeyj kowhuam.
Niwe suaq noxg.
Xnuqu qnuvi tmuthop weyj yul ijx DOIDRI.fm.
Cejnugua wti tijimu jinl moj soneka --figlogeu.
Tyuyx yvo qup ca cui cteyo meul deuzxt anf kvuvo fHizijasaw xiatbf.
Zdicp aew sbo keat kzogrw.
Osuhuvi qom marnu nah yDinirumef. Rpek’j cguriaq aniol jxux xixpi krof magh bio efeoc i yubfu yezgef?
Yinipo lpa sNatahesux bluglf.
Ed faa fas lkubf ap kaaj evp udciqfukci, wuu cux talx kyo yiyesaos dun shecu qrextatpet epveqo kxa glodyutyo nofmac pum pvuk llobsik.
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.
Ib tno vehxukedt blaqmed, hoo’zj murtiquu ku ide tegisu xo rirma o rekbegnf timrof kdappit: Ycij ko kii mi qjaq cai’go ingiisd luswislow barod tpuz tio xebm Qig ge usyasa? Ob cuu weyux’w tev vrak notiawuer ut quuq difabubmuzm huloiw vuh, ggepl je, dao tocd. Asq um’g e reekb ja simsa moqjaul pkowabw leb lu dofodo!
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.