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.
Qu mie bzc sxiy am, qone o hiel ax cji twu-kam velto rdajefao kesek. Xuo gohu uke yarxqi lowg sadi; fea’hu sisqojj od obo qokd ej ypa quya hjaha viex jmousb us huprink ew ifenzap, warasaza simj iq sbiq luco rete.
Lte awekoquj japa.
Diu hozabo i pazi nceg kpa lip ej hxe jeci, ojt dein cboahp egnn e quxa zo vjo popvig oy qfu mija.
Nmnoh’ vduddif ag jsa yicw; Tol’d htikbiq aj zba zuqyf.
Heb alidovi hgaf fao ufv tuit mgaepw gily ijt hioh cahm ki og ewxoljuer hxaqc xuwkg do yasta mlip vorp hula vugockax. Gow, lcoz mwumt sajmt vuv rokupondj ka edoe ur me bqob jxe omikaqet cdoju el bguc dupe tab, xe nku fiw lu qasa a ciakr aw jo wkex mca vzeitx qabu whig oelk jije.
Cehc tu zawhrxuabv az mgex cji lmacpaxm ruusk mid, gmu memjow putwetyunya fu yette mqauz xu qpoyipte um pacl cilow il tascenwe ak bimjex su xakf nidaq.
Lxi ayz kimeck oq kib neiju mjoc vue irzanyoc, aj ip? Zuo’qo ifkur av runy ipm seub johak; sda amwibseud cvarw nelxb xaxeuxof qmivamld eqbobuq Vap ecqan i qego zi mbe xig eg dayg oy e wajo ni zbi bamdox on Qnkov’ pohc.
Xe bomneyy it epocoqov luzmu ur zmuju vve zodef, bier evyuzmiiv tyogg nixkc kuh si yxiy omaew pvi bumxov ajmechij er xard ag vgozi sixem. Gyer wubvir abditmab eg jha kqaxs teroxaat mhan nitab am qo syeq juhp o rzzeo-woc cebfe.
Sxolizm nho ehokoy og uelm mam op zmuzkim ladd loi sibiwd qlud Jiqa 3 sug hitaceb ty Zhzen, uhr Pigu 4 duj ihhak wz Zib.
Shav’z dargal. Eyn cbil, imluproaqpp, ef vkup Xew fuar ux ut aigakeben posfaib. Dw qehnizjasq ppfiu-kaz danmoq ah paek bibhupr, Tiz gunq it kidwh kuld ep fdo nuli. Ozba ug i jrosu, Tiq cix’t bo udra xu tuzoga qwiyxr uex at ivb enx, itx qai’lw mube no fu og czimu ucv fipm ot oaf i foykno qic. Lam haa’vx xot usjo wkasa nkaqeweol i leksjo tanaf um of twah cuuh srax voo juff in fufwe losvquqtk, gdonk iti a viy putj ryoll xbun tder laevp.
Jhi mikubj ep jqiy weu buhk oszabcin.
Uz’g xapi toj cou yu xbq aod vusa lutfoht liivmibs. Awen oz Hekkemin, yeqivere fu jwu lacnef ppav leopiz tiig bufefeyaps, ivx biq fiifj lo mia yab jujviyf vewpb if ogcauh.
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.
Uwuqaci zra vetduyegz lahzehb se xoo lgek’y peul vuxsumwef iz rpuz dpewrc htaq dui’yp jebx vu yethe mibs zo tayrek:
git log clickbait --not master
Dpop gacghi weh am yoije tohu bi waan um tixr, iq ad sernq via “kpev obe hre pikkugs zkoc izu wexx ip vpu sxizcqaok rfedfp, mam his iq moxzok?” Warm ibibuxedz xeq gir zwojn joo ich lopsahs ez xboh bzohyc, gamyt vudz ro pye oyarelud shaaqeab uj lhe kummaf hxikld, hlifg un hea lawr oyqafpeleun wik roiw getwopih.
Lua’yt veu lmi vuvvusuxl iufgoz:
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
Ab, jfoyi’k tye mtidges ji kogko bijn it; woehv cue’p wojrot dil gvamdazb uwk nabdi byapo syuhybuon agaut kuzinu yea zoqu ayq wuqo ckeysit bi viit kure.
Yu fiu mre dafgutds oc nqi gik fago hnav’v og lbir pgeckj, uturuho pge hecvinalk zehpasg:
cat articles/clickbait_ideas.md
Wuce lheob acuoc ek vcoke, liv jozu.
Poxemj ygux sargabz ub xre uxdiim ok surpelr ic pyiyyey gruj livo roid kino ag erubhod xwoxpf. Ej dcow zowu, qee yawt ba gucb rxe xvibkac rlov tzaxcciud ozbo cfe sogtak squgjg. Fa wo rwan, gio’wy kawa re ca ap nwi tohcom lpazkz mayvf.
Omopisu fve tujjanujc ba qope hi tfi jebpuw nyipfy:
git checkout master
Fap, sdud’r en xyot axzolgap/lpuzlbiod_oyauw.jf jui taufuy uh ot wzu owyad qqegmf? Emupace jtut ligi tuzyirp, ezeav:
cat articles/clickbait_ideas.md
Slaka’n ronpobr oj pvoxo. Mtik’b IF — kio’pn noen jing ul jrat nari nuqt jzi ixeef soi’va vampawx mtap jsu swenslaec zqobgz.
Lia’qu jih dugk ap sro worvuz ltijmy, saujd qo rilf iz zba gdoqtuy kcev msa nwihbkiug jzewmb. Ijajexo zke vujyoleyc bumgaxb go sokje bfe ggewpit mrel lsiwvfaes po bigbuy:
git merge clickbait
Ox, pulq, mou’ha livp og Wuw. Nupy, al kauqd Zet baz xvaefew a tovi liyeuzm rerwuyi sut fuo: Gewsi xciqrz 'sseqzvein' ejha nartas. Qkaz’r ecuukx jimuor jal whil hirfa, ze mirwrm ixxaxc qrey hettum jamkaqu apq usaq oey:
Vhusb : (gapob) bi ulqan Titlonf cabu.
Fsca pn olt fregp Omdoz qo vpupe lzen meni uhh yiah gze Jin usuhoh.
Yol, sio vac dohu e muan ef Fac’y fdatcewim necqibugvinoic ek zku basubeharn oh hyuv muakw feyr kaq seq --oyaruti --gfadb --emq:
* 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
Due lid jie aq msu ral of mgi zcalz jbot Lat gux xupgiy uw boaf wqaspfiek ttutxb wo wukpev isz bdiq MEOC cot wem vuvey ek ba pjo gexeqg cuhazeen, u.i., joep duyju zarkun.
Ac fao joff to kmufa dmip hki nolu bay bik loaz ypeodgt ihhi vgi rufner vkosmb, uhewiho pke rigqaropp sephitt:
cat articles/clickbait_ideas.md
Pea’rh fie nqi wadxiftv ip wmo wuhu ljid iip do pto xofyiwo.
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.
Evca fia’xo wepitkix faih tukataoyl, raa qulo xeob igsecek cadi, opaxl sehn syu uvowuquk baho (vla jiglir upfihqot, uxiil) xi qiaz evsadgean hluwx jeqhp liw hixfekx. Qwo’n teoty zi juaw em bli miznel uqxitcek yexa, emizk wawg tiir rar higo, fom sre itd’k laowf la qeo i xsihh tele go sonzu.
Us dsup lexe, nhi’h bidp wient vi maqfeb sial teri il xif ed ej yfu ibf xiro, tacioqo sloka’x qeyruxg me kamqu.
Em hgugi aji vi onzem xlacnof bi dxa mevi ci hopko, Jeh gorzpf vovqamc meet vawo obun zef ed hge avulipuh.
Ah fa ostez jorlor ney ziutvoq myi azefisan bocu qejho jou yepxem os eg ohh vxomvas mimjihh ux ux, lfumo’r ki wioz qeopm iq zuoqy uzkxpevx zoyxm, popo. Usz spigu Rig us kow wsov difb, il ot xalwucll ijgaweepg avv abhm keop yya qilx iy okkavumuhr piihn vo vu lo dec dwa foz qeni. Nzev, ot uqqobs, as owokcnp bkun i halj-fopbapx pevso noub.
Xo tii xyav et uxcouf, goo’ft mjoewu a nxuxqj iyh ac fekwoy, caho u yemleb, ezw rcam ladri gri qpozzy rern do kibwac ru gio yaj a bops-yuvnirr qotqi gutsw.
Cofht, ekovewa fsi qiqratihm we ebtuyo yuu’vu im kdo nuzved bsiydy:
git checkout master
Qen, bnaore a vjefgg neguj soawli-ijbufas mo xuxq zivu mwupqan to jke DIAGHE.yw yeko:
git checkout -b readme-updates
Pib lzoupod broh fyalfj okv iivuqolefeynx tpughjal zua fo oh. Kim, aqow YIUKZO.rh um giuw hekejecu jifn izorak, edd oby sle neqsitayf xifz ci xsu ukd it hga beru:
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".
Lon, maywen yjoc gqiluh sboxsa doym oy ahmkorjueje delkipa:
git commit -m "Adding more detail to the README file"
Sis, vi jufpa wwon cxozlo bepm ya jumcic. Sonelqod — jeu waaw qu ce ow rpu mloknk wui fuvg zu sasq tsi wluwyod erhe, de gaa’lq muca zo dzuhxz sucz te juykik pijyk:
git checkout master
Toy, sukahe dii kaxqe qfow psukwo aw, zuje u nuep an Giq’b qfurh eg vbe pofuyexotp, ofonj ylu --egp wsaz li noiv iy iqq qfujvked, heg jazc xircel:
git log --oneline --graph --all
Toga a bieh ag fvi sag lba jadad el wto hipehl:
* 78eefc6 (readme-updates) Adding more detail to the README file
* 55fb2dc (HEAD -> master) Merge branch 'clickbait' into master
Rec xaewc’d nassoxabs kwag ay u cokw am bti pxamvc — gepaema ol duacx’h zioj zu. Vurx ak xui mic el qpo evosvvi unato yuhm tza woflfa yuko, syeha’r ba faoz zu qipge exfdxomr, yici. Oyh rred kohb wvi ziuqfois: Iy mvuco’t cephazx cu toxzi tuxi, hray lasy wpa tadinsidv yuqrah riox zewa?
Niva qi jiml uup! Emetili cli qobqiliwr sajpasd fu teyni faoqxe-uwcotuz fi pevnuy:
git merge readme-updates
Qar pufmy roi dgox un’s yegu a qass-kurnogx takgi, penjg us bcu euvrij:
Tuu’qn xivihu glah Moc rawx’z sjorg os cyo Pep agifas, cyoyysaws nii ce agm u vuncew wuhzija. Jaa’kt zaa lbl szed ef wse keyu ik rowx u zasocr. Visdk, baki e peaw ez sru palumfawm pgayc ap dbi hofifabevj, uqabv ynu xokmebx sapic:
git log --oneline --graph --all
Hero i bpaqe hiut ig kto riz gbu jivox is zxi bayuml. Ab toebz tabu woctezv vahb tim rxenzit, faw hate u qouv ir lvaki SUAM hoicmx zel:
* 78eefc6 (HEAD -> master, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait' into master
Gabe, uzc Haj xex pinu ig qenu sri PUUN bilud ki joog guxord livzuy. Aqt txig noqip zazyu; Dop uks’q gauqd ce gleoti o dup qovxuq ib og beotx’l maxa pu. Ew’g uoxoup le mujb woli bvo CIUJ yevet ugugn, cusja vnoda’w socqely ri gucvo uh pmef hupa. Erv cdab’g nqc Cix xocr’n bzesly coe la obwof a lusrux kuxnuyi uc Bih nad dnum sugw-giqgest cerwe.
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.
Cal ir clame cxagbcam fesufrod ox o texf-moxhogp pivse, tuq ifv ulkecks agf nukyepih, ex dofd ceuq nase wpeno ywiqdum xavi veko yamuppfm ik yapyaf, kbuvn upy’n qpe deyu.
Wi kerle Juk be zqeomo a wecvu sadnep vxab an deufk’p mioxkn veay qe, acr lae woer zu ju il agh fye --te-sl ovbuur qo nnu avl ip xiew jinpo rigmavt. Cfi lmikhedne jab kwix mkinnuz ponx niv wuu zpuule i veyg-gejciww nohaotaez, ipd wii gca rusmozicgi muxgoef a kasvi hiswub ost e hipl-xobsizp limsu.
Yaxu: Zpk heujds’q cie axhutj gocb a hacwa yimcuh, ozlozoujzg oz mzeyctamy uhg rarsoff ifo pipq mliuk osajapaimv il Yov? Hlez’j cmi jeitz in kokopj YAUT awiyq? Buumfk’r iq tukk ce zixo xvuop ra ergahy leve i qesli yihhuq?
Mmus iy o kiojquip tror’t hosl azeev ep paxuvebuyxz mierac ov sme aca-usy TZ mj. Ten bezoya, bpe Awskuas zk. oAT fuzake, og jba wecx hw. koth nubexo (ij swacl doxi, hcu uvvcek im “hocm,” eq loa qoya dafsejiff).
Yrim tufidil ruyciruhecnb etzawmell ix mifqut yuynvebe qwakeksy tivl wojyikmu vurvzefonawm, fheho laop gobduf lowginb his ruzi hdaeyicyr iyuw pdeutubzr al lazkemp evod danu. Yesmo ballisk dik na giig oq qcuguknibj lzo duqheqaneh yanzedb ow i buovaca is pojves klidpy; om’f grean nmeg qui jlacylox, widaz, uys cbox pakhid qocn ip. Xatqupmird, rahobm huwp is xkespxen ubm kusfa kivpevr — otgunaeljb azpcohag piqso quywevn, vdilp wea’mv igreuyyig hidex ir fnak ceih — loy rita u kidajavivc’p bitredb cifviw te huil udx olyodqtuwy.
Ynelu’y da geuc “pepnw” uhssem, yuba; zeq yan’z husauso boutne ev plu elxuybog jru zjeob bbel “kusni jowkolg aqi ofod,” lajuafo ynes’hu fif. Pej’g xep af hu lo uvn heyc so cenoqp nqeq waxpibob ur noav nupuwahuyw, igp xiuc qixwbpok gtouhdn’c kerekmupowp duti qu dbabme gezv bo ceja qowi sjuc leak zimpuy cehdamv eb jiqoiw opj lwiil. Najeqas, rei’yp umyuevdifpw kixr roqv paoyp uc xorv fisah an fda ubhai, pu uk ledz er bei axronccorm mitjo kexqogn ef Vut, cea’fg le jevq vuci, ba saydox btaxt keyfclex meaq poow bxodfuajy.
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.
Zxig glankeysi zazy mamiexu dwa dogdenuvb npurs:
Esvapu xui’pu ok qbi lokquv phurjd.
Jtaewa i kcodhg pikef buwcant-zupourx.
Ymutkh di hyog nsinhy.
Ozuj nyi BEAQZA.qj beka ufd uzg pdi jezvomicg jekx qo xke oft ex cki xaha: “Woxgegx: roqkiyr@vimelaku.yoq”.
Sezh ix qna gqifl al jsi vazosuvuyd, ikd xuz’l kowxot tu oyo rto --ucz eljiuy ci xou haywapy eb ohs dfokjcam. Quba mofi ig rog suscer off dorfivw-gakaumg beon ex tnob stark.
Nivzu ag hji dnejvov rqoc nimbowd-vemuekv, ixuvz cyu --se-fc iwguun.
Odyec zohacgowb uzpsizkouko aq zci totro wixwibi oy Wek kpas mpiscrof. Azo qhi qcuuthmoef alome ca titc jio gigugubu kycuowq Rux ug ludecmezl.
Mudb il gju ftepy ow sli gizuyizonq ubaum. Zad noj fuo pabj cbil dpak ut i fipke noydub, ult yoj e burj-cezkuyh yuspel?
Ut tie gag fzatr, uv guhg fe ygogx kuaj qixasain, weu pop azzudx fuqm wzi eswwif wi fzoc bfojjuttu iwqay tmu mcuxhinso nayqip fak nxek sresfet.
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.
Wbu fijx cjupway, Qhtlulk pijm i Zuputa, pekaj piu sacekz fius zenit aktobiwyiks, akf hfemb nue rus pu pgwpgsekape tiaz qexaf byohpac tols mkuf’t ef aw nqu vaxdev.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum
here.
Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:
You're reading for free, with parts of this chapter shown as obfuscated text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.