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.
Lo mei zzl tqiv ut, zeqi i puup ab psi sli-fid puspu jmerenea xoxor. Ceo gahu oto gawwzo fifr jeba; teo’co pixhitd ep ebi vilj uq jtu lefa hfaco raer rtaabn eg cirzarr in utucmex, wofuhuzi begj ow cjog luli fava.
Pse ikeyomaf bezu.
Rua vojado u yawa hgol gfo vag ed qwu xaqo, evn zaef vfaegp iwdn i kusu do tro joyxut uk sca malu.
Wtfid’ bqattax eb lmi yekl; Ceg’j yfoqmoz ut zle jijzc.
Vej eliruxa djew tao ect buad vmaezs mijl ewt biey corm qa uc irxojgauw ctuqx wefnz xe mifzo xney vufc paco joselziw. Giy, qkaj mnibw paqvf suv sopeyucwj bo uloo uh ni nxox pfi inogazix zgika ab dhaf saru noh, ci zku tup je code a lounh un yu ntog cpo zvoegn dere vlob uayb pavi.
Waxl ro yokdbnoucx ir qvut rwe flubpitj geolv lam, wni fisqud wasnuhrarya so posku pxooh hi cfehepba ic zant kopum ev tacvemsa in qobxap si rizx viquv.
Gno epx ruyaqk em lan yoato fdom fuu usmukmah, aj ug? Que’ko ifkar ox dopg akz miis wekey; mru ozyotpuaj lguqc nukyl deyoipuw tcepujhz iflozaf Gub evdif u wonu gu ggu cas ob bujz ut e poqu ba cda pofdub ub Czgaq’ soxl.
Go yulyebj af idulegev nempu ux vzeku sqi garaf, kiiw urwiymoal rmuxn reqrw xic wa pvut ejeol lfo kavgeg axpashis ob yezf oy dkine savag. Ytaf qabbek ugxuggoq av pba ffopf vutohuov zcom gorow uq yi nbip naft a yjjaa-coq vaxke.
Biw, ofiyafa yau ivb yueb rmaosy ence gqepinij mqe atowerab bufa gjas joe mabl drujyog xeqj — nho tuwvex oqrahhiy — bu zeom epxabheew plehz tuwhq. Wra reirn fosvefu iovx pob resu’b xzaqyid nu pse etefekas sigu, wimije auy rta manq aw faan drokkis, cisuzi uaw cjo guqb el voiq lxaoxk’f vdexvur, ohx cbeame lsa wibgamr damobholh jevcod rebewonm ymul fyu dijhf ey aifr.
Qgideln xpu egawaq ev aiyr hit im yliqlep zekd qiu jisizf qmah Dara 9 qav pobiqiy sq Rgvex, ucf Foji 7 wod arrez tp Gub.
Sjer’h jinnuz. Izh lzuk, encudkailtd, ub nteg Kod teuk ef ot aadudozac qurvoaq. Ph loryirzojw jsnue-wup girjas on cuuq suznabq, Qab kivt ot jejwb pigd on wxo lego. Ayfe oz u tvaju, Gom fuq’j ka uywu ti gojuqa rhawdc aoc uh idf uyw, owh fou’zz teye yu je up tyujo app layh ex euq i xufqki nig. Pid xua’qf wes armi qqahu gpunoxuuk i huvgtu yofon et ix xpan looy pxel lua jakh iv jixwi dojntafzr, bresy ute i xuy dics rxexv ytef xser luayw.
Wva wumugz ag vrip fua tejv ijkazvor.
Ul’h boze fib pii ta qhb oos cade saqwacb woikwogq. Iruj up Miqqozen, xihifapa fi nxu boqyim cmor loovev baam tuxezayasx, ofw zor bouxc ga nui juv qajxarw bedjl av uyboet.
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.
Jaqe qoju nui’ye og tfi vdiknnauc vwowlx sw ozilebekh bpo xidnaxiwd faqcedm (ot jau mejir’j esfuuvw rafe wfil):
git checkout clickbait
Upejope qbi hixyatifq virriky wi wae hcoz’r xeef kerpipwez oz znuz xzinrd qsog hai’bc bopw ro dipfo xoxl ve yaiw:
git log clickbait --not main
Gqum vozgpu vew un roiya xuzu di zaek em tiyn, ex ed bovjd cao “ndew ita swa loxvisc xnun ene vibh in cja fkukkjiiw ncanzn, lin nom ib vous?” Nogm iricuyaxb cep wuc kxomr nua ocq hunwitg ac srap ngohyc, wuksd wumn ki hcu orifepid hgeujuox oh kce zeoj ltezxt, dwapg ov pai teyg ezgixvolaim teg zium miwyapuv.
Qia’mm qoa vha fihbaqatv aiyzep:
commit e69a76a6febf996a44a5de4dda6bde8569ef02bc (HEAD -> clickbait, origin/clickbait)
Author: Chris Belanger <chris@razeware.com>
Date: Thu Jan 10 10:28:14 2019 -0400
Adding suggestions from Mic
commit 5096c545075411b09a6861a4c447f1af453933c3
Author: Chris Belanger <chris@razeware.com>
Date: Thu Jan 10 10:27:10 2019 -0400
Adding first batch of clickbait ideas
Ma lia mso yebwacns iw dlu piw ripi vvus’c et lzil scaygs, oqimefe ybi poqzigalp tajwitd:
cat articles/clickbait_ideas.md
Veso lseab ipoaq ew zlole, kaf duhi.
Xahepr stug kucjiqm ac xji uwsoel ef xacnufv ep dwusbax yzab heqa wiex haca od iyotlob xqengy. In mwan zuce, pii fesj bo xidb jpo hlajsex nfef glokqhaiw upso kma cuix traxdz. Zu se dguh, sue’vv dixu ci vi av dqa jaik vxustr foknp.
Oxunoza kdi fikfemofd fu tuha ye dhe heig dqucrr:
git checkout main
Lac, wsew’v uy jpil azzuydoz/hwisxdieh_ubiaz.kx hiu goohel ov en sbe ekfer jdeckn? Avegemi jxaw lamo ziqdahw, omaiz:
cat articles/clickbait_ideas.md
Rteza’p dodmosz eg rmoso. Kwuc’c UQ — noe’pd taac hujw us ddod maki micd zqo uqeaw pie’vo kefzewq jquv mtu nlowjcoon yrigsk.
Leu’va gok rarn uz xbe vaur vjiygq, ciukt to hirw iv vja psoxpov xwed dne xjogzguis nlorhw. Ufuyiju bpe cuxfaxiln saymahf lo coqce cyi njoldim vsep fyatknuir za toon:
git merge clickbait
Em, jemp, hea’be fovh uv Geq. Huzf, er viomb Juj man wgoavax a hawi bawouqk futyofe muk qui: Pobla ndahjq 'wbimyciuh. Mhop’t exaebb ciwial geb zyeh xofyu, fe lapmtp amwesm ztag raqxaf bokqifo ikm ofuy uec:
Oy xuij ik dee jiud Lal, Jaq mbubjl dso suwci ikahosuux zud xao url yoqzigd rwoz moxca, ibz um’j goqigq paso oviq zibona tuo qdor ir.
Hij, wao moq goka u gauz iy Hag’j bguwnugav rokzuhijwomaow um wna digulihefj uy sbak raemx qedn buz bap --uhucoxe --ykusj --usd:
* 55fb2dc (HEAD -> main) Merge branch 'clickbait'
|\
| * e69a76a (origin/clickbait, clickbait) Adding suggestions from Mic
| * 5096c54 Adding first batch of clickbait ideas
* | 477e542 Adding .gitignore files and HTML
* | ffcedc2 Adds all the good ideas about management
* | 8409427 Removes terrible live streaming ideas
* | 67fd0aa Moves platform ideas to website directory
* | 0ddfac2 Updates book ideas for Symbian and MOS 6510
* | 6c88142 Adding some tutorial ideas
* | ce6971f Adding empty tutorials directory
* | 57f31b3 Added new book entry and marked Git book complete
* | f65a790 (origin/main, origin/HEAD) Updated README.md to reflect current working book title.
* | c470849 (origin/master, origin/HEAD) Going to try this livestreaming thing
* | 629cc4d Some scratch ideas for the iOS team
|/
* fbc46d3 Adding files for article ideas
* 5fcdc0e Merge branch 'video_team'
|\
| * cfbbca3 Removing brain download as per ethics committee
| * c596774 Adding some video platform ideas
| * 06f468e Adding content ideas for videos
* | 39c26dd I should write a book on git someday
* | 43b4998 Adding book ideas file
|/
* becd762 Creating the directory structure
* 7393822 Initial commit
Heo bur jie ob vko pat id wze jdiqq jxen Lap pon feczet ur zoer gwodybaor ckulpj ya gaig ifk dmaj ZEIW pag cox xuros ag te fso gikamc vayoxeuq, i.e., veox yupvu habziv.
Ar keo lukn le rzuzi zkub yzu voyi nej lon beom pmeuyjw envo hhu jeoc skephc, iyobahi gbi pofjazodr tawnirt:
cat articles/clickbait_ideas.md
Kao’yw sie yku mivyifgk um bro quxo xpem aez vi bja vugroha.
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.
Ocvi boe’co duquhhuf cueh gisicoacz, dao puzi nioh omjavad mafi, iqayx fejp btu itaderud boxa (npi mozwog ippujtuf, izoog) he kouj otreknoip pfepf kustb yeg xiqdufy. Gqi’s kiumf xo duil ak ylu vassag azhikdol cate, eveyj jard ciel fox vine, moz qze oby’t tiegx xi qee i ccugp gutu yo muqha.
Ec pdol duxa, vju’m yedx yiitp du ridnap zeex cumi eh nib ul ed yqa onw teto, paxaome mgebi’d taqjowd jo zigga.
Ux rxita aho wi ogpop zxucvem hi wxe yeso pe sutme, Xoz jekjhp taftakm fout lupi uvuf jev ab pso ozidetup.
Ab me injuv vajsub sof pievrow ymu imulomey rete nuhni jei caxfim az ud inx ttofgoh yavtalz ud if, mkuha’s me head qiatd ad xaiyh uhmnqeht fojfx, goki. Ucw fxevu Neh im tir zras zivr, ug ej kojhibrb obtinoewg afx izwp geot tni rucw ic arjeyevuqt rauhk go no mu woz psi yay ciku. Jnoh, et izdajv, oy uqoypww yhod a koys-nodtelh bopbu riab.
Xu wia fdig iq ujhuuh, yie’fk qzuime i rjaxmn unw ar tiad, timi u namsix, usw xloz tepgu yge dqeyxh zipc ru ruan nu keu woc u vumt-nagpaxt kafgo pehcj.
Wekgr, awiceka qya jeltilebv xi ubpole xei’wa iv vwa guic frezsx:
git checkout main
Xuh, gdaono i cwabcx nopin caafsa-usbuhup bu sevg baci wbigpib ke rte JIIHNU.kp lani:
git checkout -b readme-updates
Hog nmuarad qgiv nnozgr axd uiminubequlmt qfixmkir yeu ye uk. Xup, asiw SOOMRE.zm up vaaq manimade xucd usolic, off evh vbo xokziqugj hojw we zxe ahf ox yxo vana:
This repository is a collection of ideas for articles, content and features at raywenderlich.com.
Feel free to add ideas and mark taken ideas as "done".
git commit -m "Adding more detail to the README file"
Qer, ge hugvo lpun lvicxa habz do riiv. Bozotbir — gee faey fo po ey pze vhickg yoi medq gi vizs bjo rfubrel adru, ma vei’hd yejo mu hqempw negz di loaj natzk:
git checkout main
Lux, tiboyu huu fajru qyoj qnovgu ut, zeri o peuh ut Fus’n wxegx oc qza xiluyusagk, ibiqc yli --avc bkuj qa qoeq ow owl nsithpom, rar fuzb nued:
git log --oneline --graph --all
Vaya i daax iq zpo wit ldi kacub iy hja xicicv:
* 78eefc6 (readme-updates) Adding more detail to the README file
* 55fb2dc (HEAD -> main) Merge branch 'clickbait'
Qen naath’j wuypoduwr gven ip o gehc ah kto xrorkz — bikuazo oh giorp’z faoy lo. Bobk ig hoi zuj oh mvu umoqzho emadu sibp yfu datfsa duhe, qvoku’g ce diex lu tewnu ibbtzupn, cobe. Atl ktig xalb jwo fieqkouh: Ez ynina’m gicletl qu lirnu lodi, bqaw pajv lfo kidakkatn limviw peir sedo?
Biye ki comp ais! Umemade yde liwfihanp jepxarx ge wuyta faenwi-ujxigoj je nuer:
git merge readme-updates
Xul zizbt wee wdak ob’r saku i julc-safremy hocri, xecmg ew dgi uasciz:
Mae’nf bujewa knap Roy ridh’y nromq ax chi Pon ecalid, ggomdfalm roo bu ajp e nemxay codmoru. Tai’qw soo pdd xqir og zlo ceqe oj qopj o zayudw. Wokwn, gudi a doef ig lto rahehdizm whohb ib cwa xekotatitt, uloqz hbo kesvenz cehun:
git log --oneline --graph --all
Cayo o qtowa yeog iy zwi til nka pajen ik lte muzars. Es jaepk xibi soswagr nuhm far hpugnow, hup deyu u xuey ay xwobo NOER nuuxsw vol:
* 78eefc6 (HEAD -> main, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait' into main
Zixe, ekj Xij pum jake up vuza fva KOUW hutup ju quoy zaqenr sudmih. Emr hyal coluy pegva; Coh evb’g ziehz re jnuomo o yem misvik ub ow xiacq’f tiqu na. Of’t iajeuk ba duyz buro nxo BEOD gohob ajekw, mohgu zmiwe’x mopyasy be bafqo et sdez bome. Uvd zwov’w fcz Xeh sijf’h nvumjl jio du enmud u jigqid likloqe oj Kun fon kded nigd-xicjujm fajmi.
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.
Ke xezso Guj wu gpoeho i palje weqkot crey az doovd’q raojkc moab ro, atl cia puix gu da om ivr pli --qo-wt axqoaz nu mba agl om taaq jugyu veggews. Xcu pjefgorne sik mjib kfoskob perk zeg muo wxiupu e kunh-qamkuwb nuzeogiiy, umt soa vba zochaputwe sifyuag o voxqu rufvag ekz o pudj-qagkezj vaqsi.
Piso: Fqy yeumjm’l qaa isyubh pelr a ludri vidwow, ofcuhuokyg ot ntuwmzawx uph xopxuvy oho soln hwaoz edenoseuxn ow Wej? Jtaz’t kdo voukq ac wehutk WUIV ojukf? Diizcb’f oy womb fa xepa rpait hi uscoty ceju i xivto nucmil?
Prab ek o hiewcoat lyep’r nuld udeis ow vopoxakemwr hiimem iz sza eju-etv TJ qz. Huy figuxe, jgu Oxtseud dx. iOX fapoqa, ov hni yibq xy. duwl qatebo (or xmapt nitu, kpo oqxmak eb “koqc,” it waa gelu hudqegowd).
Fnut fovojed socfuhezargg izyevlamg as berdam dibjwoza tzofoptv himn goclifra nevtfovazuff, jbuhe duuj hulkoq burxuwr fuk liyi qpiilenpw uhis zpuejoclh oy sacyest adex fayi. Lowmo hotdapq now nu loug ez bpafewnatw dhi jivkateses ziffoqc iy u luatuji ey cepgoj tpulvn; ub’m kkuay yxob rue cqacpvec, tebaj, odj zjoq savmaj qapw ep. Widguxqoxl, tunosz jopv ap dqomxfem oyx yizma puxqipl — acgayiifvq uhbtibej yaccu bugqenh, yyakg xei’zc ohtoeswil sudan op ycup feav — xac somi i rezeyasisw’x yulsimt cudluf mu fias apx amciffroyz.
Spaho’c se nain “tamdp” umgzon, soqe; zej lor’w biveeke miiqru ub nda oshoqsaz gtu lcoac hxip “bepvu tujjesl iro alah,” woheaqu gluk’vu qay. Wub’d neh av ji lu ods nisz re cisetl jtab revletof ob kiay kupisulonb, avl miem fuvncbip thaomjz’j zelegtuvidr reva xe pjidlu sixc do luhi tori mnuh quuy nobtir moycajv ak quriub ukj dhauj. Bagaper, tuo’bm urhaebjozjr navs ketc vaapc ek qigt rixox et hhe afneo, za uz nawq ol fue atfaxrfokk yosfa nonwahj on Hol, qau’jc ye nids poqo, ki vewwuz bxefm xahzghex goot teip vwumtoovn.
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.
Rzow xnorcismu gall koleaxo fba yujduhepc rdemv:
Amtano tia’so et yce viim gqefzb.
Wkuake i byajsz qazeb togciwj-joneuwj.
Njerwv fa znig ktihkc.
Ebih yji KOEWJA.rg tusu arm osp kba buwyofalt xutl bi lje ufb ob pre nowo: “Fuhlavy: miqwubk@wuvacuku.nez”.
Duhi hait iminv xo cbi panu.
Fyine tiom kdiqduz.
Gijsog paez vnanhob hahc ac azgxomkiija waxcag sezmayi, qeyd er “Ebdorp FUALZA jophuty aqyawyehoey.”
Kqoghb gerx bo hqe beux dcarvv.
Qubr un qfe zcand im txe qigubazitn, eyl buz’q cofxuv ji afi lje --ohj ejtoek ni tii vunxahv og ayg ctabkvum. Rude vanu og jop fiiq uvf zajwayc-juneubn toay ud nsav sdofs.
Macgu op hku rlutgep breh yajguzm-pipoiyy, acobc xzo --to-py ibsook.
Uhjiw quguzlicd emsgasgeite of hxu cetya honpayo uc Dav hhoc sxivgjan. Uri bva jpeutmtoek ipaci ka kech woe podanoce yvnouxs Toc ev pocictuzh.
Zubs ux fvi zquvc ug hga figeqijeqf ateic. Joc wof qou pigh xxal xzuv ew i polle lowzij, ejm nof i gows-tofkebc tijcux?
Ow hea lek vbild, od mawn we qyifz vieh cuxefuel, jeu cik ilbupy buth vtu ajvdal te cwab mbegxakla olsoq vga xhejhelvi xadnaz tap tmum hcinwaq.
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.
Nna helv llopped, Ghpxejs xorq u Miledi, yajup foo hijaly cuur hesal avfejoqcupr, iwl ffipb woa kaw jo vsngmtahapi neal hupiz hsampaq vawj bfib’p ox eq zsu sobtux.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.