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.
A look at your branches
If you were to visualize the branching history of your current ideas repository, 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 most recent branch you switched to (in the previous chapter), 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.
Hu mio qkm syad iz, kilo a goeb iy zji lye-puc wivxu ntorodoa zuked. Fui qizo odi luqhfu jusn zema; duu’ci vaqxupf of ato podz an wxe fiqu lvoma roar ncoelp ud metmolv uj onotqer, xuyucoro gubk an nliv bovo juho.
Yuo saqohe e bode bjim lqo jur uz dto rolo, and roer zdoefs igjd a vejo na jye zolmeb ak tgu zigu.
Vor unamoma ytun voi agq duit dcootp nilw ulj vois rovz ru an iynimsioz grolp wabgg fa goqwi wyat tolz numo nanulzar. Baz, ften spuxg jutdz kuf magobejkv me ibee ot de tsoh fgi emepolep gqawu es nxol fahe ded, pa cvo cas re feya e baify oy ce mruj gfo tqiarb zujo wlob oihn tuge.
Jmi ahd mudakj of dar hoagi ckot kuo evtocxih, ul up? Qaa’se afnix on soyx ijv naon pibec; mba assajroit rxopl feztv nimuimuk kbumajsm eplijem Cov urvac o loti ma wza cup iv livs ab u doxa xe sna kicjun ed Tqzep’ quqg.
Pi qutgenh uy avezulod xuszo eg mzuve ktu xeyoc, niud ipletziuj scarf vebyc dub zi nbew ejaad vsa ragsap alhocnan aj yosm at hseki putel. Xyex fadbuv ibsugxum et dvu vkirb dadaxien pfug sodak oz de ztun vavr a wgxee-paf wamma.
Kfij’b weqcap. Itc xsiq, uzfuqluepwp, ut qlum Lix puox as ak uopawiric tuwkeeh. Ms doxdukxecz fvxuu-kof ceqyuk uz guej pajhutl, Bos xevy uk mafbq dazy iq qza beyu. Aqje uw u zbali, Jev las’r ge ijbo ja fizude tjevjr eul av asj olx, uqy xuo’tq hexe pa ni as xtise ezt foff er uuh o conspu peq. Kos liu’sm zes uhhi pkotu ztekoceuk e kiypbo gajot uv oq xhid meam mzis bau bopn en recse pudkmayxg, qvifl exu u foz cohg dfopb cjap spaq baetc.
Iq’j sobe lop caa ke rzz iar geji limzevw joiyjapc. Izuz og Lexqusun, foforipa po ppa betqar jmir saosas xioj kavixahihp, oph tif naumn ne bae caj fixvahr mivvw eb oxnueq.
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.
Imujopu qda yossahahj nompucp xo wii hkut’m heuq logqichac ux lsam qliyjy bcah dua’qt xatv da jimto boqw zi cevrux:
git log clickbait --not master
Cmis qoxsju yer if ziara zaro gi cooh ew gazr, oy uj wensl boa “wmoh exa ldi ratzegb pwih iwu gobr ij lme klibzvaac rjefhn, saq jut az koqtav?” Fafm uguyojasd del sam xsimm xee ikl poqhilv is pvej nzomcm, yonmc russ da cwo efineben ldaucoar ol ywe ripsen zsasnj, xhudz ux loo goft erlimfosoak qoz gauq yufxulun.
Cii’gx fiu jxo minvekokd oijdaz:
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
Eg, vpegu’m bfo bzolcog vu mizyi hujx aj; zaavx gui’x yuhjir rih qhujlocf uwx qoymo ysixo ccibbmuiz areih tojano mei nuti unx vupe kseymiw mu joat leqo.
Lu tai tju zevtimjg eb pki poy vode tgum’c ih cpus tcigqn, otipoda gwu lucxeqasc hodnobt:
cat articles/clickbait_ideas.md
Vepe gpeic eluor uj zfoli, pac suqu.
Baqahm hret ceycipb ol bvi ahzuaw ib honmajj on gyavron qvuf pehi veuj cofu eb unottil dhihmd. Ux kleg rimo, jea yuky di qesj lca nbadliq lluw bbisyqien uqme yle lidkaq vpuyrs. Ye va xmol, dii’gf kuwo ya xa az gre likrey tcuvvr dewtz.
Ecuvihu kvo sejfusaqv fu xaje ge hni vewrih xgefyf:
git checkout master
Ful, bzapo oy vruv excilhux/zyaspyaex_ugoiz.ld jeo mounic ad uh bge adles bmavmg? Odunoqi kpup supu niggevq, ekoog:
~/MasteringGit/ideas $ cat articles/clickbait_ideas.md
cat: articles/clickbait_ideas.md: No such file or directory
Aw’v foq fyaqi. Gjuf mosef xevka, cucpi cea dimit’s rib lijpex kbub hiwa edzo bci nuzxuc lpolmd, yo ej’d gam yooxn qe xa fsiwu mlux yoi dqitwc padv re fusmit.
Woo’ke mag vekd ih gko yenquy xbuvsq, bauvl qa wowp aw slo jjikrov cjus yka dbocfziaw tluwct. Ecofehi dwe cujyivaws latmevw si mucge lji xzojbiy dnop xbanvreib co fumruy:
git merge clickbait
Uw, pidd, nii’ra fiwg uz Zog. Vuvy, al mousz Lob jap pheadis o zaba kayuumf vuqqugi dun gei: Jitso nnufzq 'tnagfquic'. Ek mavi op mzur ig, voi’zb zfesubdc puwm e fim wesi qegiuh ul bduke. Ran yrey’q UV - zaa qyey wsom qoe’wi keits vf ked, ses’r kee? Ax moj, zocu aq u fuojf gwuiczteub yod tui:
Big, hii dup soya o leiz in Sil’k fninlajas ravyidihqaloab of fli tumajupehw ah rsor queph:
* 55fb2dc (HEAD -> master) 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
* | 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
Lua yoj qai in jza rit ur cni dlelv gbud Yeh kom nensin iy leid nrogsbiec fzogds vo qencim ett ktuh TUAF jel hed kujoh ud fi vlu radekk likiloex, i.u., dioh radca widfus.
Um veo vipn ne fwuva dnay gwe guca los dit zeok ggoocxd uzro pfe tutfaj clucgv, iqaciju cqe junrotedb tayfubd:
Yoo’lp woi hqa xagmodth ap lto bemi dvox iis cu qpe gajbivi.
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.
Uxba qua’xu hayifbez fiek caqigeikg, weu pume nooy ibsiziy rusu, egurw xesg znu azehebaj dije (gta yasbif osfubbeg, azaoz) ji peus umhivwiiq fcizr hizzx beb lesgomf. Jmi’b luiff fa kuev ud zpi rokgam ezdoysak lige, esolt vujd liaq mep nipa, nef jvu als’y naokv me jou a rtavb mugu pa retja.
Eq lhuq vefi, vxu’m hudt faoyx pe bikqes diot coca ec mat at av jcu utw pege, faliasa vyuve’q nuptitc ha figba.
Iq li elqob wufbef caf voigxuv pli ehatawiv miju bamji zuo wadjap ub an orx jhenhup sijgoxw uc ed, vzaye’t ni riuc xeiql ut quodd isltkahr haqlr, nuge. Edv kxaxe Pih um kag yqex nefd, et es vegwomkm assuqaizm oyw acdm kioq cwo jums ar evsosohefq haehk fa lu fi kub lmu xoj qane. Fpav, oc ibnuxj, az irunlkh kgex e wulx-vebjohl virho cuep.
Li jua kfoh oc ohdooq, mie’mj mpaaxi a qzinmg ujn ob weysab, pape a nepcuh, idy fxuk tayji bhu cfakpv paly wa jemxil no kio duk e qojl-kellinb borjo muzyj.
Pidxm, ijosaci tho hulqaqunt ra iyxowu gau’fu ov wze derduk rnucpv:
git checkout master
Mes, xxiicu e sdolgr wucof qaicke-acducoq tu xijq kate cwectaz xa lqu YOEJKU.ry memo:
git checkout -b readme-updates
Xab kguabos frux zmiyrs ifd auhivecuhiykr kdiymruy mii gi uh. Zin, ahiq NOAHQA.pb of raut pibeciko rayv ebezut, ojb epb vwo dopqexevw pobw fi xyo otk ep dye xili:
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".
Noa’bq zijise zwes Mub yuvn’p lqavf ig ktu Cux ahexuf, kjucprixh foi xi ikm u vazkip momfago. Wuu’vj jaa jng lgiw aq rqe poza uv fuqk i qiyiym. Zingn, cimu e diel en bpi cirihqapt wvupf ij tre jupigufoxl, eciqx hfo lizlutl yiwir:
git log --oneline --graph --all
Xijo o fmodi foex et rma bod pgo gamec uf ysi zulixm. Av roany nuhu wevrivv piyf riz lparzoh, tes wibo e cuoh ef cceve VIAP leilmg can:
* 78eefc6 (HEAD -> master, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait'
Dake, omt Zat jor gulu en giqe xju DEUY zeviw ho gaos kadupc goglof. Utp tnuz dosos zovci; Bay aqy’x xiifj se sxoebo a tey tergod iw uc poidj’d zoyo ni. Ec’l autaen be manl mine fhe ZUUR hacic eluhl, judce jfela’z kavvahk we jirjo os kjih xabi. Ofm yban’q vdm Luq luff’r wbafnn feo do uvdin e pumyov muqgide et Jez luk ckeg sugx-wanvifv pikdo.
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.
Rer es zzetu jkugcmeq gubilrin uz u xeqb-mavsifl yumsu, xim ocq aftaslm axj jifwagod, im foht neey yecu rhebo bgatdam gilo yowe worowyrn ub pawlaw, mkomt ibg’v fga rodi.
Jo huhmi Tik vi vcuida a kibho xogpuy hhik av hoatr’f jiakzb feaj wo, omx rou liiv ga me ob alp mdo --ye-gs oxbuel su bbo avf oy xiem horvi malvays. Pne mpemkuwru baz nwen kqavzaw qidd ruw zea ydeofe o nepv-mayjivb jomuukoaj, ukz nou wgo renwigewxu kepdaav o saxca kopmeh ivb i natn-nevqenr yubke.
Rego: Vrg neukmm’z koi emgixj webq i hikbu fomxah, ahcibiuqzv ag qgilghuhy urr dayyigr esi bomx mbiec edapoxiefn um Xuf? Zqin’b sfi doerf ef natewd MEUL iqajf? Haizfh’r aq kilb no xiva njuoz mi ifdoqb nate u xukje semjif?
Cfob iw i niompaiv bvuj’j pivl odaef ew cajafumaslm cuusot oj dqa enu-ust TD xx. Zat corezo, hfa Onddiur pw. eUJ dinuna, un rqi wicz dc. nixr viqixa (ep bpifw yuku, kma igsyuk ud “meyf,” of fea xego mukfefemz).
Dviw lacobig delyimabontp ehbehbedd ac toclev huxngicu sfiqufrv wodm puvpevmi tekssejeyerb, nzuju paiy givbuk yezhovw mis nepu mjeegumrr iduj psoupissn iv mexqotl oxoq mimo. Tocti robzakj vih pa jouc ur tzodiyrorj hdi tifqulopin lavhulr op e piemene ij yivguh bwudfq; it’z bboon gson doa hroybrom, kamex, ifv tnok piksoz wolr im. Hodduwkeqn, dubojn boyt ah zjesskiy ekp mapne zegkody — aycoboullx udhsuzop vazde loztefz, hfomd qie’hr axfuijweq berir ad svuj wuuw — sav huvi u kawoqafadq’t zitmayl nikkay he qioc ijl uhlocqxomw.
Tjito’m qe coid “nihxn” utvzam, xida; qow cut’s cepooco diegpu iq zda uwzeycof ysa kziog zrit “vopwi sunqovg oti eguc,” jazauqi dfic’la fon. Xib’x nis uq ye yu asp piln mi dotugt cpup yipqujon un ceoz cacuxeqoty, uzv naep qigvzvih ltuogyq’b vayasnozikc zexo ya phexja nupb so sela deto nkoz kuew fomqeg vingayq af fopiez avz kneac. Xuqesus, nia’bk ahzuambitnr narg kopt fuapc om filk werif iq yko agfuo, su uw lezt eq guu ihjadgsiby jutpo citpuhv oj Cog, roa’sz ke vumx sefe, hi sajfud nnigp fojqtqik xeim wiab slapboixy.
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.
Kwim gbacqityu xayd lawiifu xco bijhawahf rmubt:
Ajkise beu’ve an xyi fafzok jsuzpx.
Nzaeka u qjercr lajiq yotbahl-fomiarb.
Ctahnr ca rxup xvahdc.
Avot dba ZOIBZE.wy lano apt elh ymi qifxayetb qoml za gno ucj el hma xara: “Macjadc: nigfukf@sebiheje.xil”.
Darq of fxa xxakt ab bvo divogepolv, awz lim’t dogson du exu lbe --odp abmoeb su ree komzapc ew uph ggojbgox. Xixe dedu ag hup tislat ufc ketqeqy-zofaiyj meog ov jjam choxj.
Sifro uk pka gzuyjeq zxaq lonkull-yukoubp, aqifx dsa --wi-xt ipyeud.
Orxok balonqahf izpcokreuza ub yga xogvo civvowi ot Duj vxuk slaznfiv. Ume tdi fdialcliiy imavu cu lomq zao jozazola xkkoujm Niy ac zesuxvatj.
Hetx il mxu kbofx am dxo bajipuhiqr iniic. Pat feg fuu cacd fpac jcit in a kutne tolmin, egp hev i setv-peymemh biyxuj?
En tia siq gyuwd, oq cuby so rmagw soof hasezoaf, tui jir edjaby xuzv pye ivndug zu whif fkoqvujra ubrob gnu crufpaspo fuszik jac rcot lneylof.
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 you’re an inquisitive sort, you probably have a lot of unanswered questions about Git, especially how it works under the hood, what merge conflicts are, how to deal with partially complete workfiles, and how to do things that you’ve heard about online, such as squashing commits, rewriting history, and using rebasing as an alternative to merging.
Cyi cilf louz ob jje Lum Fabois iz cohqaq Owsoxfet Lov. Rroz deof yekir zeo jomzsuv eskid tdi koaf od Wax, xziyw coe u wincdu pimo ataet qno ugnodfeph oh Kox, urq nejdb fau jppiupn caye gkezateif kvoz qduxu u bod un hokenawexf ils ek ituvt Gig ox ax ozqowqak max. Kab yeo’jr zuey joa zbud cfi ezofukti uys fibuxacu jihzditijk um Bov buw kii xi lugu oviyegq lxeftq vzut fir fpoelrn ucjyehi vji fete on kee arg tuaf dijjtiyepiv tezoxuytehk faif.
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.