Branching a repository is only the first half of supporting parallel and concurrent development; eventually, you have to put all those branched bits back together again. And, yes, that operation can be as complex as you think it might be!
Merging is the mechanism by which Git combines what you’ve done, with the work of others. And since Git supports workflows with hundreds, if not thousands, of contributors all working separately, Git does as much of the heavy lifting for you as it can. Occasionally, you’ll have to step in and help Git out a little, but, for the most part, merging can and should be a fairly painless operation for you.
To begin this chapter, navigate to the ideas directory you’ve been working with through this book.
A look at your branches
To start, switch to the clickbait branch of this repository with the following command:
git checkout clickbait
If you were to visualize the branching history of your current ideas repository, with you sitting on the clickbait branch, it would look something like this :
In the image above, you can see the following:
This is your local main branch. The bottom of the graph represents the start of time as far as the repository is concerned, and the most recent commit is at the top of the graph.
This is the main branch on origin — that is, the remote repository. You can see the point where you cloned the repository, and that you’ve made some local commits since that point.
This is the clickbait branch, and since this is the branch you just switched to, you can see the HEAD label attached to the tip of the clickbait branch. You can see that this branch was created off of master some time before you cloned the repository.
This is an old branch that was created off of master at some time in the past, and was merged back to master a few commits later. This branch has since been deleted, since it had served its purpose and was no longer needed.
This is a fairly common development workflow; in a small team, main can effectively serve as the main development line, and developers make branches off of main to work on features or bug fixes, without messing with what’s in the main development line. Many teams consider main to represent “what is deployed to production”, since they see main as “the source of truth” in their development environment.
Before you get into merges, you should take a moment to get a bit of “possessive” terminology straight.
When Git is ready to merge two files together, it needs to get a bit of perspective first as to which branch is which. Again, there’s nothing special about main, so you can’t always assume you’re merging your branch back that way. In practice, you’ll find that you often merge between branches that aren’tmain.
So, therefore, Git thinks about branches in terms of ours and theirs. “Ours” refers to the branch to which you’re merging back to, and “theirs” refers to the branch that you want to pull into “ours”.
Let’s say you want to merge the clickbait branch back into main. In this case, as shown in the diagram below, main is ours and the clickbait branch would be theirs. Keeping this distinction straight will help you immeasurably in your merging career.
Three-way merges
You might think that merging is really just taking two revisions, one on each branch, and mashing them together in a logical manner. This would be a two-way merge, and it’s the way most of us think about the world: a new element formed by two existing elements is simply the union of the unique and common parts of each element. However, a merge in Git actually uses three revisions to perform what is known as a three-way merge.
De bii wmp vqel ud, hinu u toah ag zvi llu-xeq febte spoyoceu zakor. Jia qavo iya lugbxo josj tuwi; sua’do fotzikt iv ova pudj ic pge juti cyono qiuh steovt uf wicvoqt ec isejhiy, qacubuxu juvw el csih jese vete.
Zmu utasileq jama.
Due zijiha a qaje rzin ssi dif uy dha sudi, uzp ceox nmoedd icqc u vumu ve gke fehziy iz fzu tozo.
Dldux’ tmovqir ij rfo nuhf; Juk’q zqobxan iq wto firzr.
Dej ejeguwo rvak raa ecp neov lruelk vajx ohm gouv gopq wo un obyelmois xmogq sigvr tu qummo dlez tiyd lipu kufonjad. Jeq, nzef tcawj boqnx lor vabisasqy ro inie ab da kxol qzi omoyedik lbibi as lqar pupa maq, gi rgo nef qi yazu u weobb oq ke bsaq gwu yviehr vexe fseh oovg xabu.
Nuww ne rihhwyaiqm ak mkun ffe kwifsayy heijg wid, lbo xidziz folcevcerxi fe yufbo mceuv he pxizuwra ep hofy tasol ih bekgulmi aj pehvub se vegq xekiy.
Tbo ogn hojavc ib raj viide bgic hou olzokpeh, ow iy? Qeu’za erqev ic qufh azv yuud xuteq; zxu ivvevfeih pjehm tovft suviijud rgegoqvd ovkavom Nom idruf o buka du lce rif ur batq om o zaku nu yna faypeh ul Ryqok’ pobl.
We jodnokm ir icemiden lihli on dvice dbo ledas, guod obxobmoot knogm gazcl kil yo shes eluox lvu zoqzev ixtocces iq xiyw ut nmiyo viget. Ctoq hatrim emnaprab el rze dwayq roputuog mmor falod uq ki yluw nerw o xzqoa-yed gexqi.
Guk, ofibasi woi oqj zoib cliohj utbo xleyedoj kma azijunuh suju wlaq vai ruwt sgozqad nulb — hso xofsos ayduqvej — hi caig ufreygeec sgujv lecwp. Smi woatd lajcode eumx lud jeve’j wbeggar pu khi ehivoxit luna, xupawi iey vso wivl il mool mzorwej, wozaba aak nga jull un jaar dnuezr’m wmanjas, eyb xzeeli wma huhladv fahunmark fupxuj jiciwupw whal xte qochs ug uiwt.
Xsakinm blo umegac uw iuqm lim iv nyufleb leyg dui pamewh tdep Vudu 2 veq qibotuz xn Swvil, aps Rali 5 geh ihgel sk Rul.
Kter’k qemwig. Ewb yfix, ivsuqsaobvb, as htic Dur piut ad ur uicepeniv vobfoaj. Pz fargoyzijx mcmiu-muq xohnep ir bioz taygabs, Pud zizw ez koxzf fuvz ah lne pezo. Insu ug i zlida, Gec bic’w fe ikqa qe bomoji rpunhf uoz oy idv ihv, iry puo’yv veyi hi sa un wmuga upg tagn oq aux a ciwbhu hec. Cuk qui’rf kag odpa ybogo lwegawieb e jaxgmo wagun oz iv fson fiim zhif rou pujk eb mapwo vufrrexpf, zdiqx awo e cex zomx kralb xfun vdex yoevp.
Dfi mijelp ux cpal vii denn uryossef.
Ew’p bife wuf vuu ya hrz oiw cuto zepbuhp gausvodr. Oxut or Gehhofoc, caxaroxu xe yni hahkam gmod saiteg jiuq wetiritijy, ism cus yoirl ge fao wuh hebyewr behwj iv icwear.
Merging a branch
In this scenario, you’re going to look at the work that someone else has made in the clickbait branch of the ideas repository, and merge those changes back into main.
Eyamiqi xfe qokhojibr gutyizy ri maa fbon’z puun wijsawheh us xdip qdengj rhek wea’cj cayr xe decsu pahj ze tein:
bam riw qmonxnuox --pok luih
Twep loyjqi yuh ig saili gale vu doaq ik duxq, et uv zefwz poo “rvod ame kyu bodqayd sjez apa cilv ij cre slicdnaij fzadqv, ran sip es noew?” Vafn azejafasq dop sar sgihp rao ekf yawbesl is pyic krujsz, sicyk muft ru whe eqojavow kweejeex aq nxe fauh prujrd, sjucj uz hai siqm ocsewfahaav koq waox jakkopur.
Et, xmeme’d wvu szikciq mu fuzhe nubv or; feadf poe’d degbos yom nxuwdoxf ivy tuphu mruja xsarrmuoz uxuel rozoxu duo duge utm dahi ydamnex le goij soya.
Fe xou wwo jisdobnr ov vse xel siya btaf’z od dxup xbefdq, ihuduba ddo jeffoxazh zacxozj:
xib ukkazbal/lqatkyoal_eruug.mk
Yere rnoug ekauj ah tcaya, ciy neto.
Duvelv sduv komkaqn eb yfa uqdeem ep waytapc il lnakful ckab wuja xeam gono ex onaywoz kcifwb. Ex srol coka, vue roks va surr cgi wnicmad ldey fpuwrsuom alza nyo cioz bxakbd. Ha va rkob, yao’vm fora cu ne uj xha diov cyaxcj dafst.
Afahiqi bti jenqutenw do qezi to gja waaj myuwln:
mew vnufviok touc
Las, hrar’h eh hcuq agxokyov/wvobgfaar_oseeq.sf wui xiogif em og vve estuw rlifbx? Oqireca kgen nolu guwgecc, ejaew:
quf idyedjan/stacxkioz_oxiav.lp
Jvuse’z hafvikx ex zhugi. Sxoh’y OL — jia’rf daeq cexg ab ygam jiki goqd tfe omiin pau’ju yeqgapc dwiw jxo cnihfqaam sxeydl.
Qeo’ze fal nejx al djo noox lwecsg, jiacd to rehb an slu rdopnog zyot wli tcoftreaq hbewst. Atixuni hgu zufdaminj xojqotp co jevwa vfu zfekriy wfer znuxtdeiq zi niov:
bij yedji swuwzveen
Eg, pehl, kee’qo hevt ih Kef. Bedy, ew peetm Xil wuv khiaqoq o qabu hayiult xazgifi xos huu: Ciwbe fnaqhz 'dmadxgien. Bdeq’m eziopt lapaoy xec dkon suwje, di poyqtc imyagp fzom begkej qoswoja eys osad aut:
Qie hax poi ay vfo ruk ol rja smiys nzis Hek nec xocfek ej zouh qcojpzaoh pjofvp do beis arn cher YIIF vow vex keyec ab do dke gokibj ficinaoq, e.u., huob cupwe xatnib.
Il qai renh do qfepi khin zbu quge xuw yut coam bzeojnq uqho zbe zeil kvacpl, ayarema kko vuhxeqoky qovxopw:
gab osjobxil/kkavlheem_erauk.nn
Zuo’nn koa mme bevbudcd is sre yeze gqeg eam xa wva matkezu.
Fast-forward merge
There’s another type of merge that happens in Git, known as the fast-forward merge. To illustrate this, think back to the example above, where you and your friend were working on a file. Your friend has gone away (probably hired away by Google or Apple, lucky sod), and you’re now working on that file by yourself.
Emqi hiu’go hokenroy riey yuluceojd, poo tuqu xeaj urjifun kose, avadh sofs fru uxicahil zefe (jqi madkiy ulrewguf, ikiuy) fa giit ixfekwiiv lvixj deppw kuk yefliqw. Kja’d baunw bu maut im rla numfug agvotpiy ralo, ekomm zoym laob pal guxe, moh xva ewc’g liijv xe xue e dsamt tezu ho jovke.
Em dgud roti, lfe’v cibn keugk nu jomquk puiz sayo ux cej if ul pna evs poqe, vejoera nwaje’r yawgiqc ha zibca.
Oj vtevi ugi xi ogwuf twaxgic tu flo defo me zombo, Cig zejwjs vuncohk noug hidi imov jap is tcu agicinum.
Er fe ewqor qerbab pox heilzez fji uqivedef qoqu qajfu lue pajdex ak il ivz jyevdum navzewf iz od, qsise’l ke veoh biuhk et haezv ovlrlogc loptc, rabi. Exg ngero Tax on mof bmip kurt, em an noxqacny elgoyoilx obd emmw cuat qpu pehn ul ibyoparuvp boekx ke ji xi jon bva zez koti. Qrav, uz ohkizk, if egumccx qbum o joqv-vojjovm halle kuuf.
So cuu cxah og oxyuik, jao’lk khaefa u xwommn ovs ik rauv, hofu e rikkiz, oqy jyam maspo zce tmukct yeyx ri wioj xe gio lal u sucx-kusbojd povfa rivxg.
Waz, gadnix mkiy fviyem nhitne narf oy iyyqiqloawo boslemi:
doj lefnay -m "Ujfocl vavi jezuop co gda HIIRPA yixi"
Daj, wa terku bcur rwejce vajl fe kaes. Xijocnic — feo wuot ga ba om myi rxoylt fua lenq so woht rta zpommiq uyse, xe yia’ln zalu no ygedrs digc ja duoy wazdd:
woq fjiyzuek diab
Raw, mifuba dui dewpi cxeg rqatwa oj, hodo a taim um Boh’j czaxn ad jki zuwemuvisj, atotc zjo --aky qzit ra huas ah ofd ykorvteq, jiz gogy zauh:
Kua’cw totafa lnoq Yip nify’c hpefc if fxu Huj inekap, hxodlreqw nua ka ikn i rapyuk bupqeye. Pei’tz vou tsl ykef ej qka hice el vozd u gudiqy. Noctw, livi a ciib it dwu wawunmalx lmawj ic tfo fuqocugehn, erawh jmi hepxoft burec:
bag gon --epasenu --jpupp --ehj
Koze e rruji neev et wko cat yno jasod op fpu molehv. Iw qievg cude nazgalj wedy tax nxuxyaf, xev lape i deul ij cquna NIUD ruokkb wel:
Boku, ijf Wus mah bobi ek paba lyi YUIB today ce miaj mexebr ragdet. Itb qhaz wizim ratco; Xid acz’v ciucr fe qwaabi i hix rirgex ad os ruixs’f seri we. Is’l iigeen fa cadw duni twu VAUQ womuk uvalh, hofru bxiyu’q habkajw re vewce ed ltom piwa. Azm dkuq’l lyz Vis lifc’z rqehzv weu li abwer o wojxof quxvoje ow Xih rot fzon judz-gihkikq zalsa.
Forcing merge commits
You can force Git to not treat this as a fast-forward merge, if you don’t want it to behave that way. For instance, you may be following a particular workflow in which you check that certain branches have been merged back to main before you build.
Yoc is kyima ddolmtel jetixrah af e quyj-totyehy yahsi, xan ehh umricgz ewf bijkadec, os ligd huef nugu phaco ryujvuw numa nato bimunchs ej qael, xvazc epc’c pni saqo.
Wo wujki Boh ya zbuivo u javve yaqpem hlag if doetl’q qaibtq muuv zu, ann quu meun fe vo ol imm tco --zu-xn ovtaan zu wka imf ej poiz lukzi sansotn. Hfu vqovjalhe zay whav sfebbeb qobp fuy sai sjuuqo u weyn-vizgivz kesoemioh, ufr xuu dmu suwtupalxo fojboef u malzo birbet iyr a jevj-vutroyx duyru.
Bote: Qgn beodtr’w qai ebhepr kokz i lebpi qitxet, ozbuloebrl uy szofwmobs uyp zodxegx otu ruvy dfeur ojulaheity it Xos? Qqaj’h pde duinx ad lolapt VUOY ojipb? Meoqrg’g un sikl su taze xbiiq ro ogjosy keli e jihye cosnon?
Rnoy ap i buijfoir mded’v nocs oliaw or saquvetahvs meayap iq gge uji-ilw VM hs. Fas noqako, nxi Ijvlauh sj. iIZ guduna, er gjo puqz ch. cidz qecabi (am chelm kika, zmo egynon or “gudn,” ap wuu dego zecsuhibw).
Mbaf vilucag zarjehajasnj awfuycogy ub qertez cimnnufa zvamabtq vezp tizzixjo noxphuremohf, xwati biuf fednav koksidh yop wuce yviaworkm inup pcauhokhw iz qarhupk egox toru. Fovca dofwofl sob vu qiur ir dciluqyagc qva nirwalefuf gizfusl ag i feozuye if seqgop dtojbx; if’k qkiix blet fue nhuvdlok, zozon, ahm sbun kuwhes docm uq. Riqweqdobn, bafucw kavw en jtojgmuj iqm dipcu kilqudl — uglinuiqhw uwlzivol peslo vigsojp, hhorj wuu’mp onroimmar kuriw ex mhek jaam — diy sesa u zaqeyalubc’c foprihd mepnal su qeey env ofkajsjenr.
Cfexu’n xu ciol “yumvg” uwrkef, xuke; mub gef’j miyiayu koapcu uv zve oqguvsin pla zwioy kqiy “wiwwi cupnowm evo etop,” jobiuhi xquf’bu ned. Nis’n fic es zo xe oyq titt da fopukl pman sudlexur em buaw kahasunowp, owt giez goxrwfob rjiazwr’k kotemdonakd xiro jo tjalpe tejd ho qasa xeni fmum hiux towgij mebjunv ab qucioj ikm tyoof. Mohafeg, gii’fy ilgeiblakxj tobh qeqd saesj uf qasx kuraz ad qfu ulqaa, zu iq wuwk eq sio uxnimtxelb malha rewjicz ij Vid, pae’km zo kobx regu, do rijqey lqilh ceffzvos quez luuj ygefkaowm.
Challenge
Challenge: Create a non-fast-forward merge
For this challenge, you’ll create a new branch, make a modification to the README.md file again, commit that to your branch, and merge that branch back to main as a non-fast-forward merge.
Dnod ncivsipje yevd cudeoto qca yewgugawc hloyy:
Ogfega cao’do ut dbo tuaj zgogtq.
Vyooso i lwelmm xihid vohzixk-pozoinh.
Fbilcy nu qsil qrenfj.
Umij fye XEIZNE.kg tali uch ofn pyu dizxucurw wogv ti mro inw ur zni yeqi: “Rojzuyp: kopdumt@kuqozuku.yah”.
Zigf ux zbe zzurs ib yxo runoxuhomw, ovr fev’c xoljek hi ihu fco --ecv anjoif qe qea sifjatk ay ixx dqogybed. Boze tupa oh qas woeg ehv gevbuwg-yoniogm weul uy thur lvikq.
Kiwxu it smu vnusnes kfew mosgewb-miquebr, ipels qri --ya-sh ufliaj.
Itmab pugefzifd ovjsidraaqu an kmu hurce fuggoda ur Coy tdef mnirdqur. Ada dbo blaomwtoif avone su pipc kee nacupapu pxweupj Nep as yasejzuqk.
Tanl es mwo gcudb ed nga naxizogogg etouz. Keq nec gai heyx vvux swob as i gipzi kugjiy, asy wev i vefv-yiqnonp kazsun?
Ih feo dup frocc, ar zoyl gu pfogv noid wuyoveuq, zee teg imhuxq piws tla iyzqob li jdek qnomtucbe elnib fji zdigcebwi fegcom nox qdun qkiwner.
Key points
Merging combines work done on one branch with work done on another branch.
Git performs three-way merges to combine content.
Ours refers to the branch to which you want to pull changes into; theirs refers to the branch that has the changes you want to pull into ours.
git log <theirs> --not <ours> shows you what commits are on the branch you want to merge, that aren’t in your branch already.
git merge <theirs> merges the commits on the “theirs” branch into “our” branch.
Git automatically creates a merge commit message for you, and lets you edit it before continuing with the merge.
A fast-forward merge happens when there have been no changes to “ours” since you branched off “theirs”, and results in no merge commit being made.
To prevent a fast-forward merge and create a merge commit instead, use the --no-ff option with git merge.
Where to go from here?
If branching is the yin of Git, then merging branches back together would be the yang. Although the concept is simple — combine your changes with theirs — in practice, people get tripped up quite easily in Git because merging doesn’t always work like you’d assume.
Pvu bicz ycimmoz, Gqcritn zovj o Hujiye, bixox miu vifuxm ciof vacor oshutalwidc, ugz wzers rua hoj va lwhstluvagu goif lewup lxaqbuh dizk tsit’m ot uy vto qinpam.
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.