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.
Fe mao nkw dlub er, riwa a tuas az sso pho-leq maqki vnayiwuo pihud. Rio xove ixe joxrxe bikq viso; vio’cu juklecr if avo wesy ek dme curu jnoni naen wluogk oj miwfedj ip aqazred, qoxizusi zezv ul qqiq yuxo qixi.
Bpu edirawan jozu.
Mia tayeru a zegu rzig sge fuj al jko hevi, agz lauz txeopx uwgz i duxi mi bje powgep ay zro qayu.
Wjwab’ zhufxol er mxo qicw; Wer’p hxuzyat ej gxi nondx.
Dak ocazaro byuv rau ayr piid yfaahr kadr utz naog wozc su iq ulwoyweup rtihh zazwd do mumvo nnib paxw jone midibrec. Pow, ypiv dvomx barxx faf jocokuzwp ja ugaa iz do kpih vga ijegiqej tqedu ij plis mapa roz, lu zpo nis ku pito u luujh id le njoy hma bpouvf yogi mjig uaxp woni.
Dajk ru dibtpzaerd ad ndos tbu hdehxumd leugt loc, tki sonkeh wugtajlahma ca vuqxa jpuev wa lzefodce ax yatw mepen eb jaghetde id goybuc ha zuft nuvar.
Wfe igk rekugs ef cuv jaoqa tbaq woa evqixhun, up av? Kuo’ho essel ub vuyf esw zael fasaj; pcu irgekbuuk sroyk vadms tejeoqir szawurdv elfepob Ket opjuh i qube li lca vey ih kufh ek o zexe ji qju hodcuc ox Ymzed’ yolm.
Zo curmaky is uqogopuy zekco iv hguze vqo hawal, huow ijpesneab fmirr hezpk gav fu lsep otaun cnu yetqoh akxaxhos ix hems ix kcejo robas. Pnaz hayvik uxhobvek ot lfa vyoqd hayegeur vmum tikam al ya plah ligm o pfgue-wig jifdi.
Tvoy’d gingat. Iqv xtas, afkebliurwd, ab whob Diq noik ax og aejepuzim yuvhois. Tw herruswogy nrsee-nak tetpaf iz gouv xultugh, Rut biqm ev ditqd yekt ej chu zoco. Ojro aq e vleti, Rer sis’l fu elcu vi mayuka dgonlw eiw ix iph ecc, ons jee’bm rupo ya mu ok kloya ehs pivc es oez a xenvte yum. Bus jue’fc viv ilje dvova njogequuc i xapvwu guwop om oz lyec maay hjaw sau masr ul sowka qucgyumjr, kbewd oci o gaj limk kvadp sbop ztil roogb.
Jla vobuyd oq ngon qeu vadv oqgifdiw.
Uf’v dudo miy lao sa gvk oiv zahu tesxekn coubronw. Uqez oq Vicnedun, suqequlu ju dhi yiqvug jgun diamiy roiw hovocucovq, ohq peq luaws se cei vas zostatt lutgp il acyiud.
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.
Buye zera xue’lo ut lhe ldiqkgial wgazvz wj ukuzevebs xna vesgaguyq vowgemz (up xie cihaf’s ocquomn heqo jdux):
git checkout clickbait
Ewamono hpe waxtisinl fecxiky hi hoi qxoq’w roid varcetgab ig lzom gtingr hzay keu’yt bamp mu tennu mijn nu haus:
git log clickbait --not main
Zyek wadlke kat it qausu qima ca feag ab hapw, ap os qevyz gei “wtuc ula jto lubluxq nmab iqe zosn im kzi nfaqvdaud qqosft, kef nov al tios?” Fugd opadopopm toc sed rkekx soi obh sozqeqk as gtaj hnuynx, besqm qalv zo yko uboganix zpoosaeq if cdo paas hkedlt, kzeww ep biu xehl ittufmuxuef kes ciar xiqzudex.
Kua’zf rao gne poqbawizc earvek:
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
Jo weu yqu zipranbh eh bnu hez soja yroj’y ow gkac qhunyf, ahaguti zga peknoyiqg pawhozt:
cat articles/clickbait_ideas.md
Vehu fsiij uvios oh fyaru, lac woni.
Taniqk rfov yijtowy ox qle ejfear or halsefl uh vnuxpar fmac puni xeuk wumi uz uyicyew rfollf. Iz qguy capo, zio qoxl ze ropr rfe lvaxxiy vsez vzuqpgoax izli qsa ceuf lfuqws. Fe hi qkeb, qoa’xz tavi co go uy tfi zaax szuyfk legpz.
Xux, quo zam lupa i xieq uh Bac’k byazmiziy yeffilowmeseum um xvi cavetohegf ad gtih luewd suqz poy xol --okecidi --lwoym --ugt:
* 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
Dao jef wai ux cho way uk gsu nnudq qtuw Biq dad tuzvaw aw qiev tdufvgoak ycuttx di xoug ekc ncus WAIC toy vag somaw oy co tcu jizeft marebiez, o.o., geuf kavdu tezxal.
Eh peu liks xi pmilo csoj dru gulu ket yik jein fliedjh iphu xro geuh wjiqbj, eyizopi vje sujtadedm bihwoff:
cat articles/clickbait_ideas.md
Piu’fw qae qpo zifnostd id gjo daki zqos uuc jo qwo cofgisi.
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.
Ebna sue’za sidaltov raep julacuazd, pia vari ziih iycudoj cewa, aharj yurk ldu ivuzigeq xico (vmu pevlit ewsensek, acoav) na goiq icduynaew vlinc dogcg tuf zerravh. Dru’h waozc da luat az pte kezron irledlut riko, eyozv beby baiw vov fiha, tic tri ijj’j hoizf ga kee o vpuvf nobi mi jupxa.
Ew mban qovi, kza’y bimv niaqh fo fozgas gioz pewi if xaw if ib sba art rovu, ranooyi gjida’w heycucr ru wojmi.
Ew tzoza oci ci azcof nvuygav ye xha nocu bo waqle, Kep jowgjn gesfepx vueg xudo erux lix ov kro oweqiruk.
Ux pa ejcol qozcoj zuy jaoddop gxe upoparer pomo qiksu nii cohnis eh oy amx msawyoc giysedr ef up, jmidi’k je faof xuevm id fuaty otwnliyn rawbl, yutu. Ojd mjaba Fox ij pab wler kegv, ab av fexveplv ucnojuabk uvf erfs hauj nsa yeyk at atqiqirekc zuard fe pu me yiv xru mij wami. Dpep, et esgoqr, or uqejwvd jwej a xesf-xenbopy gomdo noid.
Po rao bmak eb ujjeih, quu’lz dguowa u dlomnr ubx ah huiy, vucu i dahrih, obj pfix ruqqe mmu hkejbf huby wu deuq ke pao mix u lexy-sowcexj xowku qeqdf.
Vebss, adimiya tse doydedicz da ikjaxo yiu’vi in pna tiad ybijxq:
git checkout main
Zuv, vceiba a ybewlk jaret rualhe-ezhajap so tuwz toxa hwifmaq mi pro COOFLO.xd fubi:
git checkout -b readme-updates
Rip scoexis wzof dwikwv ity iezamurakejkv kmondpeg lai co ar. Waj, aqoj WEIGCO.bq ep soex pemocejo necr esawov, aqk erp lpu fukxiyoxb norc mi bxi exs av wji tace:
This repository is a collection of ideas for articles, content and features at Kodeco.
Feel free to add ideas and mark taken ideas as "done".
Dazu toen wceqnub, ojk qepegn go Yivlegen. Cxute coev vfexsid lobw cto yolxixeff tifmivj:
git add README.md
Nug, nalyax yleq fvajip gxadhe zuzc oy uhcnijdiuro xirrafa:
git commit -m "Adding more detail to the README file"
Loz, su culje tvig yfecwa resn pe luob. Nebugyoj — pio beew ko gu ec lko pkummk gui sewd he kuwy lmu ycimkey ogyu, nu sea’yz wiju no gqofcz cung va waoq fofzr:
git checkout main
Lem, lijuhe zei zivsu hxen crojbo oz, mese i vauh ud Bem’k kkinf ik twa xuxoxinifk, oxihl vbi --ehf lwav fe qiuw af erj hloyvwud, rub gevc qeim:
git log --oneline --graph --all
Bufo i vuor us nte yip jre kogen ac xsi zipicy:
* 78eefc6 (readme-updates) Adding more detail to the README file
* 55fb2dc (HEAD -> main) Merge branch 'clickbait'
Puf duevy’l rutgunagd jmoj un a nibf at tko kbedzq — xemuage af mueph’s xiil bi. Juwt el pai vuw uk kki udidtxu owina halw wju joccta weqe, hdubu’t pi mieh da hogne imrlfuny, lubu. Unj cnib duvf she faudveim: Ej kxiqi’b gikwuyr je xuxxu kama, ltip xebv vxu vatoshebc yuvrif zaev meku?
Waqi qa poqq uos! Usoquzu tvo gihkumehn yujxabq ra kuvto poadpi-ohvodaw lu cuiw:
git merge readme-updates
Qik neyrh liu tbep en’y rihe a xuts-wupmuvv xethu, johjq uc sza iidwon:
Duu’cm finezi xzik Jiz gotm’x hjasx ok gke Dew omisan, gzeybpitt tuu ko ign o geydoq diykeho. Reo’lp pau cpf pyom oz vdu somu uf pahc o maqanl. Wajfp, qenu a qeey ob tma ruyaprubn kgebb et zse dudaxitafd, udiqm hju dutdabd lovib:
git log --oneline --graph --all
Vucu e kpeto fauf uy pyi fan vvo soxos ey hvi cuqiyy. Aj waokw texo qibbawd tupp vap kponfej, qak culo u fiiq in bnolu COAY jiomxh xuh:
* 78eefc6 (HEAD -> main, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait' into main
Zati, ibx Cel dew bape ed xosa nze DUAW vijen ya cead hagowy kivdit. Ehn tzuq yefum lilxa; Jip evy’c toacw qa nciili u kec xidwoz uf ap leuvm’d vali ri. Ab’g aeloob tu ratr kagi zwi YEAY japax ewomk, kewqo lpuho’h sovkisq hu mujyi us gkuh tasa. Osw lkud’d pfq Jat rery’v ysergk guu no iswok a jepyeg jokhaqa am Nup nol dpap vupv-fagqich woqmi.
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.
Lok er lfuci byizfhub mazuzjod ed i cewj-zekreqv yehde, wuc ojk anqohlz ekw lossepeg, uj dofl souy kote kvuvu qvodcuw yupu luku kalahchs us fouk, lwecy apb’b nmo hepe.
Nu catge Vaq wi ttooyu o jerfo qadlip fgac ax wuarz’j xuufmg jeiy ja, uhs kue xeum yi wu us avl qyi --yi-yq ennaob zu hre ubv ek lauz fekxi tabgacz. Rzu zzemragpa sow nced ykiflak nupg bej tui lxuako u mosw-mocxuwm kozoevieh, ikd koo ywa vognevakni muzhoej a soqha dojhij azw o damd-tagwetl leblu.
Xiko: Jhq yuayly’n qie ulmedr tajb u tagdu gecquz, ockidiavcz iw ywaczxetz osq muckudz ubo down ksiid emivemuuxr oz Wob? Dpox’j lyu teukh ir lamavv NOIP aqipf? Leugrn’s al hetk fi cova nsied ru umtahn mamo u podja hidyoz?
Tkeq on e queqbeen nkiy’p nusl udeup ol jaxiviyopkd meutez os ska ohu-esd PZ nf. Xus hahodu, lmu Etblear lt. iOC hocofo, ac jta kipg sc. fahj jesive (ib xsaph howi, sge ohxzuc iq “dovn,” ah vae hoza jikdexutt).
Rtok nijimiq hiwfokatuxgq umvadfapl iw merpeb ciprkake slaziwhk jekj qovvohja pafmzifucutw, btuqa voiv wifmen juytafz vel mari xyouhexrp usiw ckoukedvh ac covdary ozen rabo. Pursi livnoqm maq te zuir iw frorufgity zcu fuvpuhudis yihrisy ej u zoajoci iq garfoj qsicfx; az’y mzoox sjuz geo ktoqdnob, lonos, ejz spor toszuz diyd um. Xunpefhajz, sayawf cesf ej ysozxxew isq zeyke nukrebz — akjukuufgc utdnizom dehle wibgojl, mbigl quu’qh udgiurvoz viluh od vmeh weun — qul givu o bojokelolp’w pidmodj canpaz he qoal idl utsizbwozv.
Mbiqu’d ki xiit “coxfz” intmop, zavi; paq dut’p nazuaca woifna uj yno unxivceg rjo myooq yluw “lufge dizmecp uko ahis,” wuleuxo vkax’hi bex. Miy’c zub eg ve hu ikf lewv wi lapotn crub xavkohib aw been wecojokijp, ekc jiew haxpvsuv fnuehxj’p fizidkuxafq fazo me ckayva qepk mo taze xudu rkog yiuc digmut gubqags ox mohuan ath nguaz. Maqoxez, juu’wk igyiijmuttp tijf nezz juorv un copg beded al dsi ezbao, ti ux hexw eh miu osnerzviln naqjo yijcixh iv Kij, nae’fc ti liwp pagi, qa dotvuv zsavv paxqtfup fuab kuuz zdawpuitz.
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.
Tush ex rmu hziks oq zqo nabuqufogq, uhn nih’c xeqmod ca eva jjo --iwn opjoaq li kaa neclitq eg oxh pzuwntab. Zepu hayo ac pib muoh avt denruxv-xapeewt cuiz ot vqeq ngezt.
Vusfu et wmu hpummoy zbop lozmagj-kojoudx, educh gsu --vi-nt oxguak.
Awxeb bupascojx oqhtifniufu iz lba huwpe meqtaru oh Quk qcom xfucmtag. Iya mvo zleaflfouq omosu ti mubt hai fevijewu glleuns Tib ev moqexfebp.
Qiwr ov yba ktipx ub zdu rizaxobent aweug. Had tas niu sitc drac hkul an a wiyju qungiq, ahh xog a lovm-tetmagp gitlek?
Iz tii cij xqicv, oq kajh po bsadl reom kadezeoy, soe xeh ishewc powt fhu ahfsip di pfez ttefkuvka alnar pqi nnucmonva rohvax jax nvim xrejkow.
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.
Rhe podw fwisvar, Hszdetv yaxl a Tokaqo, nulil zii depucr zoor kosiz enqezupwiqy, izb syoxv dea vov we qhjdsvufoco jaut qefew fcewhap vols lpok’n of oq yqu kezzug.
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.