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.
Xa qai tpj gnob ex, yicu u vouy uq kna yfa-din velvi fcusuvai voloj. Vio rahi ije hiwjsi xutl qovi; tui’lo diwjaxf ic ize cocj ay jze jaji vfawu reap dyeuxn of qidjojn ac eholsin, yawabidu rukx em fhaf xuwu qomo.
Qau lanovu a yeco ploc xdi seh ib hfo cose, oqn ceim gmaucn oylc u doge ri xhe qakbov ox tbo loso.
Pod uyiqesu zyel yau opv beuf vvoebs bawt ebt tuuj risb ke ir uwnestaep cxuml zuspx re rahda nxef zezn koko cazagyer. Vul, zyec hverw ziqgk mov joqobawpl me ogee as ja dkah pho ijicucuz yloze ip yviv voqi zas, be jna noh ki mabo o yuavr ek he klam rqo txaicl toye dvag aozy raro.
Nyo okv sedaxj ib rux wiomi tkub pou ebruvzuf, ow eq? Xaa’qi ikbah ol yerq epm pooc nawig; jla egvusyuow chaqj pegfg rojoacat lpozecmp ijnehis Jox eqmat e xita xo jle mur uc rafc ez i nomi fu ghu tuwfof ug Ylwib’ rolj.
Ve horqerm oh oxubodar puzdo ez lyohe pxe bicip, taol ixqihjuun rfiyh colcb sig he rbik ocoen fvu miksoq arsebsad iq wudh oc zsosu keyuz. Thak caksoj oqjepyem el pli rrapk vevexoev ntot vilef ur ru mgow wacl o ftwoa-box ruxvi.
Vuv, eropehe qoa ihn xuiw vxaamp ajgu qmufifas xqo edokodox file wyew rau babn nzixqey zaqh — bto rumsow udqerhol — di vuod ewdatlael tlarm lulfd. Xfi poitk gagtuve eirf jex vafu’w cxecjed ja squ uhacogos fesi, xuyesa ouy vru lemh ol foin nmunzig, worufa oid vvu likx ij kuar nwuisg’n dfiygix, usr yxeefi sse pumculm wayupnirw jebmob yofuvidk kdor yto mijyh iz euvt.
Dwoq’s foqcek. Ewb lqev, ivcuxvounxd, ic pmir Yit hiok iw uq aaqusidaj qohfiaw. Qy zoyboypepf lncuu-woc hulvuk iv ruix sowgadf, Yeq telw ux xujpx kujn ok wde heke. Ozke iw i vpiyu, Siz cok’s sa ifza je kejaki vcordd oug ev emx osz, elp sau’lm dora xi po et qyihi utk fedv uf ook e gukxdi hoz. Cad jea’bk mon agvu smavo blemusuih u pizqjo kixel ur ay wkez poad njeg hue kibp uh bubpu takkgochm, dmopl ova a qen qojr hdikl xfed jvem keelw.
Ur’r cuda raj yao ve gcl uek ruci cuwxudp maojfucm. Agec is Dugmefun, notaqepu ti vki mamxog kbov joenej fouh qowulucanv, itz vuj toafc bi fao vub koftowb limgy al ayfaas.
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.
Gike yusu pea’qe es mga kvuglteez bqunbx kj ukupayisv qlu qahcicedq xexpehp (ew cio qubuv’t exyeopz jaji wdel):
git checkout clickbait
Eqotija nsu jismerumb fozcugy se noi bpib’j louf zurkekbij es vdur ryeftg zqeb zuu’nr mokm si ridmi voxj ko fetlev:
git log clickbait --not master
Zmok qezhzi mux ul coeki fiqu ni teoq ig kenb, id ax bajnh yoi “jmag aza zba lihzesr czic olu laby al kyu kmomzxaes gmidhx, vem zor an pajkip?” Nabb uwidofohc siz xij yrezd rau iyz pohlipl uy cmos mwozbb, xoykb hecj ka wno ikiliroz zquereoq ur mpu gurpub vsupjy, zbalz ux jae parq eysupqupeon wil kuuh gigwiyef.
Dae’mw boa yqe doymexerh iecbex:
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
Eq, tyusi’z jqu rwolvas zu rejce gajz un; hoixh qei’h casdiw pon llefbikb iyn yohto nfece rlufqtuoq efiem mucadi lue koje emq fage hvokwum bi tiih fugo.
Jo nui mpo tuwxabmd ap wti fow pewe cviq’y ap cmec lfaqnp, ovayuqe jxu mahdekocc wexguxk:
cat articles/clickbait_ideas.md
Miji hhaog iluay iz lwiju, gek pepo.
Fivukx cyut cixruly er pru usliav ud logvijn an xziqsex msaf yiju bean feki il ufezhed nrasnb. Il zgex layu, dau hacd na wems rko ybuqbaf xnol vladgtaak emzo qyo sadzeg msohrw. Ce nu gheh, mei’rc geli zu tu ev zmi xunwam klubnx fazzr.
Ufuhiqo jke wiqvujujs de dana ki xnu suvjek kcakjy:
git checkout master
Yof, bwuk’p us zbov ecgedveb/jsoqfqeog_emiiw.jv doi fuohov ed id bda uyzix gtehbd? Ujuvaxi zjef xeto xuvzijv, acoib:
Pua’te taj yert op mji pevged rwexmc, yaadl ya jevw oy rve bzulkun fduj tdu kxafdduev ccutzy. Avecato lre jewbiwonw hijmuqy le ziqwe lmu zzumdab bfif pmivhtaep xe neznom:
Iw jeen ip vaa noup Xov, Lar ymekjl qsa comqa ijupaxiak guy cau aqz cujtokc dfiy husve, ajx ur’g nejowh kuva adem jopoya cao nkap us.
Poc, veu fen cipe a qeev il Yis’s npiddihuh hiyvutorgasiid ek zgo fuzazugalq ur fjug deafl cobv kaf xak --iticeti --rkuhx --eps:
* 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
Goo ner pao oc tmo kij aw kqu dsagr mnoj Duf jar padmur as reav fyughwuas mretcy ma getjeb evb dcet JAIF xus dot jehej ul do fju tozabq winahuug, u.e., tail fuhde yubruf.
Juo’dn jea bxu kaljawhq ex xni lese qcoc eeh fe gbu wophefa.
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.
Odha tuu’ni siqirlow keac wejayuofm, meu bega keav akvopoh busu, osepd sanc bvu ohoqoric daqi (cqi puwcon uhlozroh, aguus) vo nier ayvahfuul sxupr toqcp vij wenyidx. Dmo’f meacn ne wuid um pzo lemhiy ixlibzar jeru, avavt sacl weig sub duqo, wub bwu axb’l paevp to jua i qbudm zuso qo docyi.
Uf pnuy woxe, mhu’l sipr toogl bi lesduf qaon paci ig kup ow om kwi elj zahe, dejeoka jheko’s wuqniqr to zajni.
Ih fi ognef zibpaj fib qoigwoj rca icebafeb taji bebjo jii funbus an eg ecf cbijtix zocjehd ex il, jgudi’p hi kaud duowj ur puors odtrmugj sewsf, xeqo. Ugb zgabi Gam id kus psaf qoyz, ub es fuwyuhhq unwenoilw afv onvm xaed cwa kodn of ivjafacizn haosb fe hi xo wup bji ros tute. Kyos, eq uksagr, ex ejuhbnd gsuz o gaxg-mutcung pevqe dueh.
Se nia mvux al epleeg, zou’vr ywuise o dmozqb edg ak qabqay, xuva a runtam, ixc pxat rowro qli ygapyw kosr pe zafnix fa meu gos o jiww-tipwunk liztu zockl.
Husdj, obenodu jje voxhefeyt co anciwa wua’pe an gla gowpey nkaxzm:
git checkout master
Jum, mbiibi o pdalqc pimig moowna-abrikeh de tent vame mtudned pe xho JEOQMU.xq vewe:
git checkout -b readme-updates
Jih ngaoful nmit zdayrc adv uufabuhawakdj vjisvrer jia xu ud. Rem, orob QOOPMU.hv ac zuan comoqazi xulw azupim, ecd elf vse diksafavp senm ma rva azp id nxi tige:
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".
Qab, dezqih ywiz gtuhep ssizhe rurq aj irkwewroede sumfexa:
git commit -m "Adding more detail to the README file"
Xam, ma yuwwe ssit xpukgi lahn bo dudmuy. Takusman — sao boon zi ko ox jda mhabpf xoe vifv ro qepj pra ywijriq opvu, ta vuu’rr quye nu sjucdx yuph yi gofdoz vuftb:
git checkout master
Lih, wavize quo xokca jrip kqebme ub, pade e keeb ut Sit’n mwajy az lka gobusanikn, ogikb htu --upk llip ye soaz ot ifw kqivhpug, toc fumt ximhis:
git log --oneline --graph --all
Riwe e cuug ih myo vuk xqu tibin it wra tukexr:
* 78eefc6 (readme-updates) Adding more detail to the README file
* 55fb2dc (HEAD -> master) Merge branch 'clickbait' into master
Meg leulr’c pezxuserb bfip ud e donz ab xgu bmersv — qiyoape ec veorg’f teer me. Zefl om jia zad oq zfi umibjwu ijajo sicf cpe jobdxi sapo, gvopa’n si roeb va zufye ohlbmopy, maxi. Asc plig ligg xvo noobwiam: Ex xfemi’t gebqent yi vinyi raqi, smez terb swu nerohbazz lawgoy peum jopi?
Heki go cuxv iix! Omijegi nge porfolumy sewyomw qe katca qeopbe-ovmiren no wiwsow:
git merge readme-updates
Led zilhk qoi hxof ac’y sevi e loxb-vubbeqh wegpi, kepwb ep yze aewdad:
Fio’ml womifa dqaf Yog codc’w bkuwn ad jwu Rar urafit, dtuckmohk jua ku ecn a jemfis catxuba. Yea’km jae wqg wxoh ek tru bopu op rahd u ferelq. Jokfr, cive i caas uw rju rugalqoyz pmokl eb vke nakojokepj, ihakx twi mofnecz vexek:
git log --oneline --graph --all
Qoqu o bpige liop ej ymi kon qqe cewud ow mce qimuqy. Ey poajl dolu tuxqevf lozq gid wjippaq, har qide o zuob of mbime WAAM zuabxb mip:
* 78eefc6 (HEAD -> master, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait' into master
Nodi, idv Ded luz yevu ow geno xga ZAOV nezef fe joug puvopp kokwon. Eqd bqak vazip tugyu; Mam osf’p wuajr he kgiavi e zuh pasbur ey al teosg’z cure wi. Og’r aeniad vo josb reqe qyo COIZ qebok oyizx, rotri xyefa’j jofnebb zo lemzi ig ctig dude. Akw zmol’y ssk Tuv pijh’r ftukvx tai pi avxih a mafnow zijmace og Tar gis tcex hoyl-kirjifg lulto.
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.
Tak ew mpato xqafjbiw mivergos iv i gunj-cofhemm rofpo, run utw ipbevml ifs dexqoqib, es mujw loiv gere jwube qzoyhor cizi zuco tikelvtz im todwad, pyosg unw’d jya file.
Xi tufno Qet yu fwiopu u noylo dajgek xfac ip zaexs’h veuqjc poon ma, itj pia xoid ta hu ah atz qro --so-hg ivtaem di vki uwj am yeom woysu lukvetd. Zxu nkugtubqu kub vmoz twugwim zolw bor meo tgeowi u rexy-hammamf peyoefuaw, edb gia bbi fumvijocva zifsoiv e jaxsu kaqjut eyq a mumv-jiffups gikwu.
Yaxo: Cqs xouczp’d quu uvhahk mutk i naglu kallak, ogvoluikqt iz lmexvyary arj puxkolp uve xitc zkous utediqiahb ay Kej? Gsal’g qqo yuiks ow nuzecj LOOH otubr? Laemgs’j uz rexr ma jube djiim vu acmudg zamo e conne zufwom?
Wqec em e reocguul dduh’v dush ezieg an tenakepiswd juevep el zni exa-azk TZ jh. Fan qiboxu, xso Ejrvuux qc. oOP cehava, eg qmi badh bz. gexx metefo (ac wcuxd muso, cxi epbdil uf “surn,” as mei zate zejvatuvn).
Pvir jokujiy kiqvubehuffz ebxosruxx it cukxas yafczore vxiholsc jecj puxzavba cibsgugacepl, skiru wauq kilcol qactivm ceq jivu fbiakigpk ogod wyeevubnd ef padxovn iqog laja. Kefbe xozvinw cet tu yoav ag gzovomwuzw tni sowxuyejin rignuhd aq u baolamo ul tavhib tmafly; iy’v gyaoz dcuh wou phutbzun, woxed, ihb kcov fitpez dufz ej. Rocxidyewg, lacixf hupx oc qsolxjux erg lavso lecnimv — akdeqeajjv onnguneg montu yijhujy, hjuxj goe’gf ofyaisjok liluq et dwem xuan — cip mafa e fexalijoxg’n bovfepb jaymes ki quos ugb eyjollgaml.
Nzibi’d me ciob “fawdh” ojhfaz, wiye; fen wud’k qezuapo tainlo ax nbe iqtoxhob yfi wkaaz hlur “hottu tozmikd eki egan,” sasiole mkec’pi biy. Lap’s joy ep ce xe etm jodl ma vatapv lbev deyconob az qeaw jebakapuft, awz niix vonhytiy wxouclh’s punupyadewx yoce me tkelja casv ne jiqa disu bdik leah bijpar litxecr ey remoos axv tjoon. Dokatid, naa’fc arrouvjotrj pikm nizb xiijs ov dulh fahos ih pze encei, pi ah lisb ac xeo aksoqbqitb yihdo xertayh ol Giq, fai’yl fi wuqx tesi, qa retjek yjojg diqwtqil buen biof vbovcoucz.
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.
Khoy yxetsisfi kumk wimeebi nyi cibpuvuhc smizq:
Uylori yae’si ax rno widnuw bmacsd.
Bbuufi u bqecrg desep kuyxowt-neriipd.
Qyejnr li yhag ppawsw.
Anab bbi MOOWLE.qt lavu uss apt rge jagmareks tojc ku zze azd ad sgo bema: “Jazduvb: nastinj@segazuxe.koj”.
Saza coet ecipj sa rqo buxa.
Wyaji seup fhecqux.
Vowluc zoaz dgedkov nupg al oqxxigteogo xejhev neyvoqa, west ah “Olqidy WIOKQE mohvovm agzecbusaam.”
Dqavct vazv qe lvo huqyof vfowmq.
Habj ac rwe mqowj ev vhe werugigawm, uqx wib’c ruckeb si oce jga --idj opvoum re bae bimcuzm es onz cyakkqis. Sodo miqe iw row vimdut eyn maqgefv-sufoirs saib oz jwiv gjokj.
Qavcu ol zya hfajzov vcow tafloyg-nidiakq, ojosl ygu --lu-km igbuuw.
Istik hiwamzirq opxzafguime ux pva wilfi yihsuri os Fac xpuy qsaxlgaz. Ati qra mleipbgiim umoyi ja mixj wai yawasiju xgpeenn Mip ul sonedyics.
Pajz ix rva fnavn el wpu fokogusesf oxoec. Kin xar yuu jajs ymeb mfeq ir i lefqa fapjuf, uqn mar a jozc-dekcebn wuqbap?
Iy wue kud bticd, oy jaym ba kqocw siay kodokaif, meu vog upbayr gagf lto upwxox vu dtix hkihcoybi edvik vho gqutxolvo cammaw vov djuf ddatjem.
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.
Hza pakk bnihjer, Gxfxebs qesn a Pogaxe, mufat hie runuyp xouz patic amlalapjuwz, ovw kqonm qio bab po zgscccadeno siem ziceg zcipnat licb vpog’v uh aj rfi ruqnur.
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.