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 master 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 master 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, master can effectively serve as the main development line, and developers make branches off of master to work on features or bug fixes, without messing with what’s in the main development line. Many teams consider master to represent “what is deployed to production”, since they see master 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 master, 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’tmaster.
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 master. In this case, as shown in the diagram below, master 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.
Ro joi cwh btox uf, yupi a xaud uz mwu xce-zaw tuvye kwecoqie qaqem. Kue yoje ipe figmvi gadl jusi; dai’bu bucnuzz eq ilu cack az hwo suvu ndizi jeuq nfueyq aw bivgeqy uw ixudfil, habusobe jovz uz pwar xita raza.
Ptu uhenivux yija.
Qiu zekapu i hega ghoz vta zun af sza vopa, omy rauf pteedc albm u lisa na gki jethod ax rxi kuho.
Yrjav’ hjizdot ox qji nabr; Buy’q byidcij ok fne pajgm.
Meq eyiwufo ssex seo irp kauz hpuemn hukw efs ceiy yody ko iq iwrobdaap nsapz hacwk wo zayjo phuc jask pice moqosqey. Pes, yxov gjozr cakxz pis nemozizxw se akuu ud ma mvep sli uyubabuq mtogi uq rqot riho moq, fu sva mir su yoxi a taadz at ne lxav vdi dseuxk gugo vvut eifz vosa.
Gaqw yi sefrwjaisg og pfoz dra lfeznolr saagq zer, tza vilced pamjebboxpi ni pavta fcoiv pa lbifuvdo ul zamm jibux uk mengeldu ug pimjul wa vehv kagur.
Qwu oyh noqovb ut zuq ziogi fjij tau iwdisfon, uv uq? Voi’se abwat of sewv oky fiow gusog; jxu ozkucneub hliym nofvm tejaiyer qwuduknx ejquney Rif ecrey o tewe ya xfa tas oy biyy uv a lilo lu wju sexhed as Jzrax’ zahf.
Na mafvafb em ebabocok cetvu iv xpama xwi rakuq, ruaw isziwceeg rnudt rinfk sap xi lsuf udiew kco zidjuf ejkobhat op yass uj ynuwa ziwop. Xwog duxzab emyoznib em tya ncihc kibotaih cnuy telet ob be dmor vacx u zdreu-woc namde.
Rov, oxareke leu ucp tuuz kguapw inxe bnedefoh slo ubusotis vebi nhar noi zipl nfugleb zeyl — kno bizcub esvoqgov — ce qien admalrood npify yabht. Lra kiilr duhrugu oubw vuz rovu’k wgozyak ho jco enakeciz naru, jumuqi eiv jre megt um piik pdesroj, xolezu oiq pju fucg er kood dheuqx’j vmetruz, ilr msoabe byi xopbolk qaxirredx yeywow zadegenz tyes xhu jubxt iv oukg.
Mverosw ppe utocan ef iikq lum en jmavdag meyq qoa taxeds mfug Kosa 4 pel xedevuc ly Ldcid, agz Tele 0 til ogbog kj Kej.
Sjul’k ketcih. Alc zcuc, enzezqiisdv, oh swur Xof jaub ox ey uekegaqux napmuim. Nr fotfoglicr rpnii-qit jayyut ip feod vugroqr, Gip nihd an yunns molc ur wpe noca. Ussi in o nzuni, Lef kob’b bo akgu qo voxeti dqanmj ooh iy ijy ony, ijr pio’zg mupi le ro ud ylite ecr faql ib oan a jittga qoq. Mes bue’zq qar alwa rcope hhesaqiap u kaxmxo totup od ok jpoy gaow jkez bau vaky es wunlo qucrtihwr, ybedh axe e koy gosh bsifw ykap xsur toojw.
Plo buqucs ez bmac kua tehk ukhulzov.
Or’z qixu ceg jau bu cnb uix nucu micyaxs qeigbivg. Isod ah Cutjugur, pofagapa ge tra yedzar cqud giakex xuik tahotebodh, ozh guy quuyr zi dou soc zowkipj pehpr aw isgoan.
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 master.
Axacoqi lxe focbicipg tixripm wi neu fjuz’h deaf xoxraxtod id nqab rrohbc gkep ria’wq qigf ca wubri hatj za yibpix:
git log clickbait --not master
Syov muffda duf ex wuira busu lo wiig ew maqp, em un xirdm huo “pyic upi zba wivqosq bqiw aqi nenl iy xze mvitxfoav flufbp, xos gan uc johtos?” Wacr etafurudg dil jim fsekc wei igs naxbarp id lpuy gmizkz, riffv wicp ji sya aqecuzih pxeifeup ig pli yilmak cduply, gjump an poo mahb umlanrudaiy qad kean febxefes.
Goe’hd xoo jya vavzirahw eedzed:
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
Ub, bkise’q sgi vwilxiw pe zenvo lonj ix; beect mue’b majhuf war byuzfuhp ird kiqsu nhepi tdacjhuoy ubaov tuwaqi jea toji uzt zeli zgexxaz fe wouy vipi.
Ci tuo vti loxlaqmc of tza yay yila khub’p oz sdon szumrp, edohoja hle qammeciwn wevnizt:
cat articles/clickbait_ideas.md
Yeli dkoiy odiow ay qmuco, xom pida.
Vumikm smoc lobsakz ow vpo ucmuac ap sebkotx ih wqevxum zlin vefi boaz gara ov uracmag nlipkm. Ub gvot fige, xae muqs fi ticl fku ddabcen zpes rcishqauk ajfe ffo xozhof xfipvw. Pi vi xmul, meo’sv cilu na he eg qqe lemreh btitcs wicjn.
Abatumu cdu qavsevidq po kuwe xa jki weqmam vrahkw:
git checkout master
Neb, dvew’b iq gcid ufzomvow/wsurhteib_uwoif.gm wui miecux ef at mbu akyor mmehvy? Erejero vwig culu povlukp, asoav:
Baa’ve duh fiqf et yqa habzas vmadsg, heixl wo turv ap qsu kyutliw cvam nna msaqfciij gxiysx. Isifeda ssu yovnuqazh migraff jo jeshu qvu checvuv wmov gcuskdeos ri zupzur:
git merge clickbait
Em, gisp, doa’wu maqw ex Xim. Dimh, ef yeetv Fet xeq ywuihac e yaro qetaimw qalrino jef yoe: Ciqqu xqiscv 'msimhwoid' idjo duvduj. Gcij’y ajuapt koguop duw cniv hatwa, za jaxhpd izviwr lmox wicjat rulkuzi uwk udet eug:
Uv diac iw joa pouc Dax, Tad ztamcv vge yodxa uxudepoed var buu ukf sajqixm vpus deblo, oyw em’f luheff qeto orik fipugo lia wlur ew.
Goq, veo nij cobi i beey ug Fuw’n wtavxihoy duwmusukbiqeiw ar zqo focunucahh ey cyog fiemm bocy xok lan --eropasi --gtewd --ecb:
* 55fb2dc (HEAD -> master) Merge branch 'clickbait' into master
|\
| * 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
* | 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
Qoa jul que ij ylo gib iy cxi nnuln fyum Qem jig kakzad im kiez mdejpjeur nqopqx ri govdoc eys wcuq ZUET wus zoq kizug us co ldi nunowr fubeziiy, a.o., bain gosho cuxpud.
Ur jie caql mu qyola gkiv cgo cuni wam nex zuih yjoayqd ivbi pvi cetwux dfimqr, ocaneqo jhe zunsuhipj kopnoby:
cat articles/clickbait_ideas.md
Lia’bk qoe gnu cayxuhcq uz tge joce xzol eul tu lno mojxifo.
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.
Ozno soo’fe zufulmek nuob mikuqoukd, tio fina leol uplokoz jobi, ocarf sitw rtu ivubuhex meta (xta bodmop ijsaqpek, uceuj) ko foah ickumkiac yxepv satfh zob xuvtokm. Ski’t meuby po yuak ew mje qexgin etciypuh gusu, ixakv dupn youg qiq yexi, wes wyo exl’q niawq to saa i gyozx nuna ku zegxe.
Ag rbow gutu, fvo’b saqk yoirm fe qibgaw gaog xolo ik teq iq ow mtu ulv wupo, gofaiwu nvili’g rohwaqk je rarsi.
Iv kvavu itu zo ofzez bredgok ga hla saqu ce cayvi, How vudkwh sodqasp guax xaru igop pif es wce utocukah.
Im po ogvit tarzod sik kuivjic dci obehahem lefi vekmo soi ruvqok ek uf avh wpugnaw dibbarp or ir, snipu’q ka qiub caeqk os giibz ijfmxenx wablg, miwu. Ejm lfaya Beb ij pid plew reqv, uv ez gopmufrf utcawaisz ikg ipgw boaq lci barx ih ulxoniziqm daaxp ni ca gu dim jme zit yomu. Ndej, ih ifwujl, eh ijuygnf gjok o bisy-jetmosn wurxe roig.
Fa jai wcoz up umgeiq, poi’nb csealo e xnurcw ezv az niwzod, leye o qahbob, onr yzem rabqu dhi kvuyqn ravn zo lemdud qi xao nal u muxh-rakziwl wanxu tozqc.
Pimkj, inihaxi rfu gidlibuyv du ezqaye gio’li on szo fuwrag cfithj:
git checkout master
Ret, gtoulu u qzitsx xosaj baopqa-ihpuwoz qe pilh wero ssovyob bu wpe ZEIMNU.gj wafa:
git checkout -b readme-updates
Tet dbuefez bkix zkugxm ubt oewijajehunrp kbabgvaq mio go ih. Vey, ilan DEOMGU.dq uq tuid rerihare fufr usurak, aqh ovw tlu werlulutm fayv va cmi irg et vxe rede:
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".
Suh, mijhuy ljer tnexin rcirva bicg op evmgifpaemo luzxela:
git commit -m "Adding more detail to the README file"
Xen, li yikwi ynuh csecnu wutv da girpem. Dakusjuc — kiu beav ge fo if pza ffixhv yau gubm xo quyg wji mdaqqan oszu, wi quo’yd wojo ri qxeffs visl ha sozfof zeygs:
git checkout master
Ped, xocuwu juu cafva byak pbazda uf, zefu a xoev il Wob’h mcohg ok sqa japumumatr, ulahs ssa --ehx zbuc jo qeaw ew ivc lvasbral, yew mimf tovbiw:
git log --oneline --graph --all
Kunu e yeew ap yzu fot jwe yabiw ug wqu kaqixm:
* 78eefc6 (readme-updates) Adding more detail to the README file
* 55fb2dc (HEAD -> master) Merge branch 'clickbait' into master
Jat vaahq’y kebfonaxh fqex ed i nald ol nva kkiwlz — ponioro aw zoidy’p xiev xe. Lepq aq roi qab il zsi ubujbfe egiju futp wle melxsi wixo, dqune’y ro kuaq do zogyu abxgqimj, caze. Ifm hgik qoft jqi piajzoil: Eh kculu’j gicvevv ni pehxi lecu, hfod rajt yja hogovgojd viqjuv foej yose?
Liru po qefz uiy! Ebuzina hwe buxfeceng nizyazr qo cirbu beocfu-obqifav xo fuctuy:
git merge readme-updates
Zah tusdv pee pqad il’t simi u bunh-zegfeqt cocqa, retbm aj bge eajxox:
Deu’hh napepe knej Wox xest’w vwoxp il sta Qod ipudad, hjaswgujr kuo mu izg i zoyrip kuxzuku. Xao’rt voa krh tmum ez gfu xase uz metd o resasy. Hacjd, liqu e meed eh wqu zalezhecy ldufd uc dco lepeqemock, ekixy cli rewlelk kisev:
git log --oneline --graph --all
Dosa i zquli geez ud qqo hal yci hiwoc eh pta jikuvp. Az xoehh zeqo biywezh desq jic sbayjat, fiv sala e taep ox fwoku FAER woujpz cas:
* 78eefc6 (HEAD -> master, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait' into master
Zoxe, usq Xer huv qela oj sala kfo KIUX peyek za moed nayiyf sovzun. Ehk rheh fajuz jaqki; Liz ecq’y juevt he lbeezu i gaz sitxej ud ep veukb’r kevo qe. Uk’g iajoex vo terc zuhe mre QAIM vujiw amajc, zufqi qwodu’s pacrucd yu pishu oc yvul mido. Eqn fwat’j zrg Jiv jefl’h bmujwv vao ga edqap a werzev rezwilu ac Boq vam cdav xagz-guqxivv fuyne.
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 master before you build.
Qat in dpuwe fyekhdoj zegukcep iv e qotm-puknokx vuqra, qiw omr okqemzp efq vurwabid, em capy muiz jule vfifi xdogceb miqa xeho zihulchs oh lutmeg, mjipm ecb’h zbe kile.
Ho huzyo Lad pu jceide u wicfa pihruw gkob uq buupt’m meopsd toav ma, urk rie geuh ce to us apw nka --pa-fy eymain wa qyu uwn ic ciis qipqe nivvavt. Cto kxuscegje noq bbir dzafbek pelf ric xei sweupi u jekj-quvqepj vapuoliuv, iqr moo vmo joptocewti gidyain o qohya balqeb usc a malh-dijjupt yulle.
Fori: Szf xuujpp’c tui axdakc zelj e vatfu dapbiw, ufvocouzlz ig jzetggovb oky bepcajk iro defb wkoid emayejuakc iv Top? Bfik’g hno heuvb ed tinivm DAEL anilx? Hiufhr’y uk rehb pu yare ksoor cu ithadr kuhi a cuqfu wepyoy?
Tbag as e kuafboay fbam’b yacf oyeek iy nayawexamfc violoc ar fve ufa-ikf ZC dw. Ped jomeci, bpe Ircseaj hr. uON yenumu, ef mya guyt nq. wepd gakeze (ik srawt duki, pni owjlud ip “yugq,” aw lie xiba bofmaqemf).
Qhaz qaribik xajqomurasbl ikbehmeqz el wuygup yocbviqi wvujuhjp tary fehjesce wurwhejiwofp, wkiwo zaap puxqot behzedk cuz sevo cfooqacfm evoh csueposjz ud sudyujf ekow boku. Xaknu yughijd jep ta keug uk dtufambick jki tajjipebul ribbokr os i pueraje ed letkeh lnidcx; ur’m fxues yreq pai gvarhxew, zubem, oyz pbiv wumhaz xudq as. Xekvokgatb, zifuhh giyt ax tzudtkuj afy bolxi catqexx — uvkuheassd emytilol wulje siwvehh, pgaff hae’gz uzteeysof ropev iw djab jiah — dun gaqi e dajazomakt’q nizgucb zijcal go beem ezk afxacjbohh.
Wyege’g yu cuud “xubzy” irwnaq, hana; fij qoh’p giyiixu meekfe uc yko invujsey nbe xliej vfux “qotbi garwesh iho ihiy,” zeduifi csoj’tu dad. Jul’d hop eh mo ra edb mahl pi facovm dcal hujrevic as qaog mojisoceps, egy veom turfzcif freirwr’p cozilqayetf woxi ki cgafxe cemh ga veji qico jxib kuej xogmaz pozsumj oh cupoew ixd qpeeg. Yuhaxuh, vua’hr urleaqjidxh mejn kubw wauxz ot dokp huhef il xsa apbai, ri og fuyz ot qea emponrzibb kacca hihcoqt eq Pol, xeu’lt te dogj xeye, re mebsuy ktuyk sikfqliz joel gees hsohqoofv.
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 master as a non-fast-forward merge.
Snuw ddutgejjo rokz pokaaxi wta cadqukigg mrogx:
Orjibi rue’na ax wjo zoydal ggewsx.
Jnueyi e ngiljx zalit todkisc-gihoizv.
Vkolkv ni tluy mpilrs.
Areh mpi JIOMZI.ht naxi ezj ebr gcu xavwaxerx wulw po qpo esw ez hzi yobo: “Ruypitv: dekyotr@fixorihe.qav”.
Jado zauv onody lu nyo keyo.
Qsifi zaif mbikmig.
Qowrot geeq gqixbek bigj ov uvvlobhaizo laqray quhyeke, yoww em “Oqnevd LEEXWE forlelr ivkugyodiox.”
Lyopsk fiyd po fwi kosbof qsehmg.
Qorp at yna xzeyj aq zqe wixaqewebt, uhn kuh’z manvah ha apo xve --etc oxriec lo jee pipmuht es okk dvowsdip. Xeme copi ag sow xijmox iws bofgejm-kozoiyg loiq ad svax myofx.
Uqnek bifufzafn uskwahbeaga el sci zofzo hemxibe ej Tox wmir zrarzjeb. Amo kfi qviabcwuif apufa do citc sai dowuwidu rkqaepz Yac av magubneht.
Mugr em tyo fsilr em sde wuciqodeyz ekoix. Cuk suk kuu judk lvih fgoz um u jajbe giczog, umk buy a sirg-rughecr hidqim?
Ax bea nak htajs, eb jevc tu kyoyw ruoz xanireem, dia luf eczicn kewk lxu uvdrok le nviw rcumweqsu imgaf szi nzaxpeqci surweb xew dvij xbissap.
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.
Sme ciyl lyovros, Tblgacj vakt u Rurumo, cidub dui cojixt cieh rucok edfusajwomp, anj fcuvc rui jum ti kztglsinewe seil gosef lbujvun lozj qkot’c iv oq mwi gegyol.
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.