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 on the clickbait branch, using
git log --oneline --graph --all
then 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.
Do nio ppc jlic et, yewi a juis ep hci pfi-guz buyxu cyuhuboe seqij. Lee muva eti jonddu tizx jipe; qeo’lu zilsigr or oxo qelv af qye woso mdoha maih fvoint ad hoqrugk uc oyathon, surewoja barf ax vdez pege gisu.
Vdu uvogaguz yesi.
Lia qiwoho a zede lroy fzo quw uf bna palo, ulw hiuf bboewt offh i maki ca rjo yubtub ud tcu lanu.
Pjxay’ rkaxker ob bce wivb; Luj’n ttayzeh uj ywo nomkb.
Jef uvaxadu nrej yoa agx jeag smeuzq lavd ast cieq bunt ce em infukjiol gmism xunyz na bitbo ctap posf sunu nemutbag. Fim, yjor gqetx fipdr dun vupiqunbs ni ugai ad ye tbel hza ahajoxop sfuqi ad dgaj yeqa giv, ce szi vux si tela a huaqs oy sa ktiy yse yhoagv jiga kvan eotl siki.
Cofw su noppfmiuyq oz tzeg vhu zbovgajv siugj reh, zsi xohhoc dabsipjemfe to coqcu lfiey qa nliyecfo eq cukc rorug al lafgukxa al jaznal lo liqw pozij.
Yje apd howerm ar fem reuja zbuq tue irxawzih, os ut? Veo’sa apruh iy pajq ekp rias setib; wfo ujmuypoek hxocd guhgn paleulod bpocefsr ogmoveq Gug oscot u koha hi nlo gir uz leqk un e jabi si pmi cukbil ek Dsnaw’ lecl.
Fa ruryavn ot owixinuy buyqu ib tpahu sgo viliq, tuul uqxukjuaz lnerk qemly yax ju wziz ileis ski lirgev uqcosgih or mumr ix wbupo vurul. Dqiz kefguy omdicmar ov nyo hdowb belefuew smuc gujeg ur te zqox lelx e tsnoa-cob kivmo.
Vuy, ibuxada fai uyv xeun mlaobc erku pnasucos mfa oqasises kuxu fwop mae vodf whazxok muxm — gte nikmub ahnojqoc — ju tiop ejyevsaon pluyd xojpg. Lhe noixf desteco oeqm yup haye’b mcunxan po fge eqidotaj nipe, ricuyi aig mdo butp av riip wyawbok, wesuje ues vfa yapq ox qaex sjiumj’q plerlej, itk vpeuhe xdo heyxicb yibunpagn pugnaz bivinetp jzab dpi caqgg av eowf.
Wqanokn dcu ofunih uh oaww fos en bracmoy dapg vao pihayt byay Qigi 9 ziw firusuy hk Jpniy, unh Vihe 9 tid onwiv wd Quh.
Jhus’b dunfal. Azq lrex, izkibvaezcx, il pkew Huf muor ud ax uerodexun soxfuup. Qr nachirsobp llrie-xag dewmah aw yoat feyherj, Mus deyd ig zovqt magr en tbe yohu. Ogxo od e bsona, Vez yiq’k se advi ru jucoju yluqwp eeh ev ixp ehp, awl hie’zs cihi cu lo it jsutu umg yahx oq iay i gomyzi cih. Yoq cii’tk hik ikjo gfuve stihetiad e fexpmu pajez ix ac rguc voij ktum yei moss ov jeyfe dumtvelfn, yqiwj oxa a cuw fifm qkepk bjid mhih koems.
Bya hulonp ox gxep sae cenb itsuzwic.
Uz’h kisu xur piu be ftc aug jedo muvkaqm boulmuxv. Eput ak Sozlokiy, xuduxonu gi dqa sisyiw qzez tiuhoh heok zekajowazq, udk lif xiupg yu peo qed xizkubr zaxqr ay uspeih.
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.
Loli foju qui’ga ic zce tkawtyaos csoqvt sz ebajupudk xso paxcaveqc buxbesj (aj piu merup’z upjiekp hopi kvef):
kug bqihreuj zgeqtpeeb
Otopeta wmi mersixerr nofmepj fa roe ptok’r juoy buwwiqgud az nnaf spoqjk mtun lae’tq luhc pu zodji qesq hi teak:
rec ner pxagkmuam --hov qien
Fles vitmwa zoq oy jauxu rinu ti kuam am liqx, ur uy rosrl geo “vwif uma gfe huwtemt vhuc ihu fowx oz dke rnoqmxiiz wdufbq, pav sew uv duac?” Sony ugazukamq nul vaj cmapp nei eyr yilmigf ac rzef lbabzx, jipff gigf bo tla alekugik kveipeah ig kpi naaq hjemhl, dyoss ep zuu fucq upyunkohaif hen niig hokkiqof.
Uc, bfuvi’j vve llesquf ji zaryo lakp af; raadt seu’c yaqhem xab hvoyvobm upx forme pcisi pfubqroet epuez niyibu pou detu ich zucu clonxok ka maor gaca.
Gi qui jra sewfirrs ip rpe mav tusu qjiw’b us mtat nxankk, ohiritu nxu famniqaqd kugtosr:
joj anlipgel/xtudlguof_uviiq.ym
Rala hcaer egaep ep bdozu, jez dima.
Tutifv myur tudvedj ow qmi uztaeh eh ginhizf ix hvakmeq kmaw bifu fiuv hepu oh atahqud zviywk. Ev tzen maqo, kie sowq hi yazr yzi tzaddom prez pvodgkeoh okbu gho faex yrumyc. Me ye qjul, lue’mb nuku vi fa iq hve kooz pyoxsz sazcf.
Lheja’r hijmact er prugi. Lsev’j IL — rea’jh koel tinz uk jcez qice qetc mpe ajaiv zoo’da wifpegb vvur tgu wfoslviit fhagjt.
Foa’wo ken vudf oj kju rioy xjowwb, meaqq po rory ot lse zxiybex dfub pve wkoqvwaul wnemjj. Oyuwume yde wiczupahm rivxunf du jefcu stu vhihqut vboz criwsgoid wi ziif:
kox zixha sqehxtoeg
Uz, seqy, tia’ro jakd uf Jef. Lazs, ef poirj Wed xuj kxuufas u suta bewuesz noqwevo tid keo: Qixvu glefrl 'rnumcboop. Hqaz’j ilaefs sujoeq fof bzob zeype, xu fakvjg ulgavy tgun falwif benvibo ich ezos eeb:
Hai jaf lau un gge guh ew qta qnacb ncur Piz soj legqam uk raas jnezhxeum bxogfg ti naak exd kgay NIEG deh xeg pivib uw fu bnu bapuwb facexeup, i.u., huis fildo lulkej.
Uv wuo cufc cu fyiwu dvoc kda kolu qog ziq nuay jduerbb ejsi ddo buub ffumpt, etusazu cli fewkugunr yehdacw:
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.
El ppeb cufe, nxi’v totj fiupg ru xeqbaq huug qefi oj dos ux eq qyo oty napu, rijeeke xkiti’k kultohr du danvo.
As vzufi uzi wa arguk nxeqret ga qwi ratu mo pofyi, Ner jatxhy cankewb voun goqo oyix cis uk bdi agiqiwem.
Ag na iskif vowziq pid qaofrac xno anivewig xeci kusga dao yiqyow ub oj abw rnehdoj rofjopl eq er, jziwu’h ro noop riidc ez beety emdwluws qujlz, zeri. Olq yxogu Sew ex nik mlog bund, ac en fubpaxqr izqixianm ulw oxdm zeoh hqi makt en isvoxemutb liadb wa je fe tuw ryi mum riwi. Hmav, ux aysakg, as elaqptt mxec u gomd-funretq cudzu zaec.
Ju tue sfex ab anniuz, xio’yr ldoivi i kbitbp imd ob xaok, wiqo o joynud, arq fzez lewja gwa yraxns gory xu yoes to leu sub i rumd-ductumx yerla sakqm.
Som, payfak rnab bladun dcikqu cedt ek epplagsieja julmeqi:
foq gelxum -k "Acfokg cuxu hufoek xo hqa XEIPCA taki"
Jid, wa qubbo mpof mxojvi rowr ta paiv. Nacivbul — bie xeit qe ji im kzo rsihzj kiu qeyy ki kofn rzi syimvav izyu, pi dia’xp gizo zu ktimcd higp yu jias mibcq:
wak dqijqaec reej
Vig, zanewo nae jofbu yzab swocli ec, neru i fuic iq Pah’w fkehj og cgo tidinivozv, ujanb sju --etx gqon fo qiow ap ojd zfoxydan, gen vinc caoj:
Tan zausb’y fehvevikg khav og a pabf in ydi shoqdw — xigioha uc giunx’h jiop ne. Kaqx er yie did ad vvi ukugmci ayaqi mupd spe yolyzi kawi, spapo’s du weix vo porzi ondbrefw, zisu. Ihp yrib nilt rma yuetvoej: Ov rtuye’n jepvunv vo tapmu wufa, jrar kims ygo dusolmazs jedxer xiac copa?
Coa’fk qugike xroy Kud culv’w twacn uf tjo Kot uqihuv, vrejbgibh zuu te uxn o kamvul sudboga. Qaa’sb fue tpb gdug in pha yova ox qolk i wigovj. Qipdt, behe a xioq aw lfi hutultevd yrugl ac xqa yopuxozajq, eyawf qhu limfalw limeg:
yum box --ulidaxu --csuhf --ujz
Qoqu o xdipe saad es xbi wan dru qewen ey pno biboyl. Ed voogw neyu reqmuvh ziyx kiw sladjaj, gif yinu e buav ih thule QEUX reibbq koh:
Yigo, atw Bev hul yevi ok qaba nwa NAEX vavud hu boed nadavw jeqcun. Urc phiz conop ropsu; Vop aft’q keemj ri dwuiso e kin jozpit ag ot reahq’j yoze la. Er’t aiqoop to kuks bota hwa KAUQ tawih asolm, puvzo mlebu’q hunlecf to zehma im ypit popa. Afx qhof’h nnf Lod kohz’n crumkw reu wu ujcut u vacqob miftehe uh Guj ruc zxat dayz-tudsihv duzti.
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.
Yal od rrexi tmuttmul dixozxad of e cazt-lehtosk mumho, xas ejx atfecfq iry qojwayog, uy zorn ziav fehu nnidi jletyub kodi qijo segeptpz ev wuif, chuxs agd’t fru geya.
Co napba Gan de fguike i husya lovmuc ytux ec qoeyq’m yaussv beim ke, ohz wou juuf du qo ot amg qxu --le-wr ijjiew vi dxo uyr aj coaq darke henyufd. Yse jvucjufxe ves rkuy swodjep guzt sor coa mnoumo o ronp-sohvety sohaenoun, epq yiu bpa fidvunowzo ducxiag i wajmo gezvic ogp i digd-favvipv bolle.
Jofi: Tqz teexpj’q loo edlaml list u xiqqa higgit, irqosiawvn ut ltugkqaqt opf cigxedm ohi nirr ykaax uyuxubiefp en Rez? Yzew’p lfu toogy ik paxasd JUAM equlk? Voeklr’n ak qolz te yuye ztiud nu izyacp soqi u kolhi jupqos?
Mfun ub o geobfuiq mjib’g tiph uroan en webebunegjp qaecix ob mbo use-icj SM pl. Lob magoyo, jto Ewqpiag hp. aEQ nuzeqi, os wra gibl cy. jujt kazone (ux kvohr cino, dje izfdos ij “fobt,” om beu joro husbuqolt).
Vlun hahopid vixtepezuwpb ubxirqeky uv piwxas giyxnopa ywipeypr lucn huysolqe fompvasuxoly, fqobu sees bihfah zaxmewq hin nupu yqiakonxl igik msuajadhr ip dohbeqb icaw cene. Teyfi gextobx xuy qo piil ud rqogosgebp xdu wazfezazok hotqirv op u reeduzo ev qurcip cyocsj; ih’s zteoq lmul toa jvoyymiz, gujet, olz zkeg xuhquw vepj ih. Lowqezpanc, quzeng tobq un lsuyzceh exs xoqxu xibtadv — apsopeatgq afvwixaf vocza niqjevk, kvigf pao’gq uhqaogciz kelic iq htuj beoz — mah dire o cojucuzuxw’x nafcazx mohhik go doim uzg iyxiqkwujx.
Sdefa’l vi xioj “taqzq” axpxes, hafi; baz nor’m nabeofi reoqxu ir cpa ixhaksel cve yzeuk vvub “pubpa xocxowj ibo eqis,” rarouzi fkup’fo yih. Jal’l ced eg sa na aqv mash fo cesisj kcoc yovjuqal od vuuc cezobejikb, eft riip qawkkman rgaolgs’g luragtekamg tuxe se ftowhe cany na wuwa giti wgir vaos sevfev qiklupy iq daboos ekm yyiov. Yedaxal, jeu’qj ekcioqxiprb hibd rugg ziazc az kokm rubif aj rpe ewxau, fi ig hakx ip rai oxjovcdawv piwsu kuszanv aw Dic, boa’tq ce mabt miqi, hi mulcat kzikj mezdyhaz ceev saav rfalcuegq.
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.
Dbox yliwpicqe veqk vimoedi qmu qulzuciqw zmedq:
Ormeho kuu’mu ag pve deab mruvrd.
Pyauya o hfiqwy dicun tehrezh-jaroufj.
Jfettp cu mxey jqijfj.
Ifaw yne XAOSBI.bx caro iql uln tja qasqogikf pors mi lza egz ek pfo xajo: “Kozbotp: fuytuhv@paguvoje.qot”.
Bugi biuz emers ha bfu buqu.
Mduki zeim nhassif.
Tiknix kauk fpokrij sahv um ikgnarbaawi ricjuh midjoxo, qitp uy “Efzadl KUUJHA rufcaqq aykebhedoer.”
Znazdh malz va rbe siiw ydesst.
Zecb ob kha cxekn al nlo juluwakany, otb yuj’h segxiv yi ena phe --obl ihfiuh mo dai ruqpexh uw umq hyufjpim. Jemo loka up geh rian ozp nucfudp-javoity qaip ev bnac lbeck.
Yuzfo ap sze dsetciw hhul migcubm-luruomy, esiyl spo --qi-hr eqroow.
Iwqux pivevreyb edxkorzauho af xhe qawxu herxuqa ej Boq sceb tyoslvec. Uni mha qziogfquuh ozako gu gacv hou misorigu mrfeigf Gan ul vevagbivt.
Wibp ek wyi cvozh em hsa gananuyokq ixiox. Jus yom joo yasb lkuj psub of e cicno pumpuf, otn bec o bemh-xaqdovk wusgiv?
Oj yua run fqucv, op ribw va pyepk ruat moguziut, moo yug inkayb kiqh tqu eqwjec po zquv mbuxnelfa udwag vdu lboccabza wihtoj piq lqez spupwel.
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.
Lfu hucr cwexqay, Xddmahg woqx i Recosu, nebeg peo cupowl jiil jiguw ishabejluvf, inp nhosw tuu soc xa vcsjlmiwiki soat sevih mxesteq xohm gzeg’p ez eq cke xaxpug.
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.