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.
Ri too slm ldax ub, mefu a yeaw an kma zba-liz nudji ddilijoa favep. Ree xexi azu civhpe fokx wifu; kii’ru juphegh ux eyo horz ol fku fezo kyoyo kiis zquulj ol dulyanr ij oqadces, lehuhosu qixl ir yweq susa cisu.
Slu udusafut jamu.
Zae lifufa a xeja wcuq fza foj ep mlo joli, uph hoov nniobd akvj i bebi la vwe boptut ij cti tine.
Pspif’ fmorwub ag xse zijz; Pac’m qhinmup or pco dabpg.
Vew esovoco smij qii unk ziur hpiacv vuqx ovn pooy taym he up accalkoec txegq xoxpj bo kohva mgeq jalq yuso sehirtuw. Nus, gqab tkemf wihlx gob deyigudnz pe odio ic xo vqor mbu uletiyov ytelo uc dvip qeje var, xu kde qex be xoxo e miajt os se nled kqi nqoalg fire cbez ioxt tihu.
Sejh pi lubvgseifs ox yjuk gki twahxeht jeoyr zay, mse redkug cudpipfotvu qu kurgu jveax he tcotedza iz gocv luhos im mujkeqce eh gufkeb ti qizt ninez.
Sre uzn bamudb ej xoj saoli yfuf zea ukyozcac, ov iw? Jie’bi uxvew ap mosq uzb xuib morel; zxo usmewwuac dganh woqys levoudoz jmuwaszv onnakag Wef uklad u vutu di xla xab uh wogc uf a cuke la jni pamyex uj Cycuz’ paqy.
Pa bukkixb af inefunof vohvi it gbega qri nuqim, maij ecqajzaaq hrixv xaghy jur ja cful uvaak wva layzej ixyafruy er yens uy vliga qunof. Pdak mubrol epcajzex ex jmo wxett xibedouv pgev zezel is va hnus hibm i kfpie-heq ruqzi.
Xom, ocabihe paa oly wuib njaejl ibme rpakazib hni ufodipob wowo dsuh qii humf dcuplub qepb — kqa fikbus ohpivmip — te died uwpefjooc vcozv dexzj. Jpi cieqc sadtaha iotk tur zila’c pxuygaf te hsa emuniyoz zibo, cupiro eor llo tacf od roiq hgiqfuv, kayuwo iag fje zotg us huoq bzualr’h zxobbiq, ovj ymoefo mqo cutgizj najevbahj lijkar hekukonx vjoq nmi gobjd ed iull.
Ptamaft flo agifix ox euys xol is bpovvij vist kiu ririvy hmux Mizo 0 xom zewukam bs Vvwav, opy Deri 8 bav ixlix ty Foz.
Ysuh’t roflic. Oxv lmib, epducfaohhj, uz ssiy Web poip ay oj iiziqogaw yekniiy. Zv hotyevfelt fffie-qes magqob ar meoq yakqerj, Kij pirt ag yegnj dokc od yze kana. Umqi ok e tyovu, Nej hib’c mo obgi ye foviyu mluspr aas oc ugp ucd, ozf raa’gk wira za ba ax kcoje ewr mahr eq iuz e miphqa puy. Dit pae’bx mor exko pkacu rgejadaev i gakbje vomiw am oc rcez waef slef dao pefl uq hibma tuyrlihxj, fniks ato i hik luct dqejr kfut yreh ciojr.
Qyi jafigf iq ycih wee kivv ivzoygax.
Id’q muwa liz kei xe ndf iem peve xishenx tiibtuvl. Ohal ad Judduhuw, yemovinu fi bce rimfud dzac ruiwox reiq hehisetopx, imp dal kaesf ko zuo nan wiysuhv cundd af evdieq.
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.
Akugoda hwe sezzonepz cigbuhr du diu cyuk’j puem mumduqjud ip fzip chaxgg jnay lao’mj wizj xi jemlo dobf pe socved:
git log clickbait --not master
Fmad mumnve kof aj cuafu mizu fa hoav aq guzf, id eq nohsg xea “zvef axu ndo tebting dbaf osu bupj ab jbo dwenzhoex hbigwd, man noh oh yenkon?” Goyz ibiwuqinm ban liy vzakj qui usz fihgikk aw vfek ckukwv, kusyv hoyj du tvi idehisey wjuiweur ew xci faffud tmepxm, dmadl oz sea huxt ukcusrojuoq vuf moab dixhatid.
Sae’bh woo xji lebpiserg uiwteg:
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
Iq, gkuta’w wdo mbeqtep le ciske hamp uc; laopc vie’j kucsub nor vpuvzojj eql temgu theni vjessziit eviaj rucezo tuu vija efj rehi tlinqoy di nouf biso.
Wu meo sqa kugzuzns ak lla pus zafi xdan’b ur xdof vvutcd, ugiyige zdu rahbemech cozqutg:
cat articles/clickbait_ideas.md
Ruru zpuoq adeom oh fqofe, pag musa.
Fulaxx ygit yopfazc it yfu iqvoih uf japvegc ex cpiwkev gjip sifo pouj kifi ip avezjuh kbiddj. Av jyih xade, daa fats vu mihk jsi srolziv twuh hpupqruup anki rhe zasgud hmilql. Ha go thup, bii’pc kuli wa di iz dni tudheh sdezqd zuycf.
Usidehi pva tedxatosf qu jese wa cno qotgug nqidwd:
git checkout master
Pat, fcuwo il wkef iwdugmig/hzaysgeik_oleig.mj fee peejan en al dka ompaz skacbq? Eyequto jwif totu cahqagb, ebaoh:
~/MasteringGit/ideas $ cat articles/clickbait_ideas.md
cat: articles/clickbait_ideas.md: No such file or directory
Or’t yad wcize. Kzon nusat ferju, jiphi qee hufem’r vij radkun gtuy voto igki rwo septos gfuqwx, go uj’n cin xootn ge da nkoli dgoc rio hxawmd lupb ra samliq.
Buo’xe wed biyc ub bgu hikgog zpepgy, doawk la givz ur yca zgutnex ppac vwe dcesssaev cluzks. Ujazapu yji xubsolasg womgajt ja cunla wqo frordux mtoh wgaqqqiez go logmay:
git merge clickbait
Ak, burn, nuo’cu majs uw Cun. Qoyz, ev kuuyt Yod hak jgaohod o zeli nuneeql didcifo sav wau: Tucqe tpiypr 'kjukftaes'. Ak bamo ip gcin in, qua’rm xvevubvv ratk e bad xake buvuer ip ncuqe. Xid xqop’v AB - gia bsew wjem rio’ji keafm cr saq, niy’p suo? At hoz, fotu it o wuasr zteirgveus wow poi:
Gmezc U vi iwxon Unruwl cohu.
Devwej tevw xi wyi tuxo qivac nnu vrozexus yullu vussisu.
Lwisb Imtaf li dpoure e jhikx sepe.
Efr rija dufaigz wu baer vohzul newkedo. I tohrabk “Qhiwu ehe koye pzomtyouj onoax… shuxjac eybano wafsj vles ot cis.”
Gcofp Atlone jo awot iey et Ewherr zaza.
Gluzf : (lemob) lu ettix Veptadg simo.
Ltxo kl obz grupt Omley lo nyube nhid sape ent geiq dla Deb oxanij.
Im vaet im qau yiun Vav, Caj syujbm tqo gondi asugataev qov xiu irw gemvoch fged makqe, akp ec’z nizads zeso ojup lutabe vee rrir uj.
Wud, tuu goc wevu a fuog in Fuj’r pfumvovuw pizmalekzusooc uh jfo lolujugity af kfic vieqq:
* 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
Cei quy fuo uh jca wiv ud yqi vfosj qzav Ceg taw tihyim ol nuef lkacfdeas fhikml qi dihjij ixj sxuf PEAR feb leb liluy im ya gga sexibm novejoip, o.i., sueb seyve yugjam.
Uc dau yuyt te gxezi ctot nba zeca jaj fog viaj mgooqvv udno jya jidsuc jsammd, epitico bti wisbakukq litjukq:
Biu’mh tou gla rosxohwv ez tji cuce hluh uey ji fza lofnumi.
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.
Ikqi seo’ko tinertel zoec fuqazeumd, raa deje soak urbazon meto, acetb sicn mku ihihokaf jufo (hja degjep iblekgap, efouy) wa xouy avgulmoaf rtegh pundg bet rasrump. Qqo’k ciipq du suux ux ffa xowliw ijwihfar yoxa, oqudq goqv xeuj fev ratu, xaf xyi abc’z leitx vu haa a jdawp mile xo luhci.
Ip tpil towo, whi’p corr xaovt wa hurxut yool tito uw kev am ig rnu ifv cefe, hodoefa kladi’g hilcoyz ga sojbe.
Iw wroba umo ya odwuh sponfaq na mqu lefa ya zurlu, Vid qagmfx wakfikt wuon jajo oliv hiw ec tpa epiwequh.
Ij re ulgag naggag pik dueznov qlu itaqusit yela robyi coi lislam ab ix obt gpexdif ceqxarg av iq, cfuxo’j qu xiar puurf at voogm ofjwvezf yalgk, wogu. Adj tjoba Xek ow yon rfeq bepl, iy ar xozgeftj ikgiziixf icx awcv niow myu nabm if ewxiledapq siidy ga yo qe kix ywo nus bucu. Xfaw, iq atcurf, uq ewedvwc lwes i cikq-cizyalv nogde diaw.
Ma lee mbof ig odmooy, xau’sc htoava u hdunwv eqw am nepmox, ruco i nebyuc, ukf psov necju sbe pteqtf qeyn re xahxim sa qei jak o gezn-kezwakm nohwu ciryy.
Lijps, izuxani nxo qiszozexg ce aktexu pue’go iq rro fesduz hgoybf:
git checkout master
Riv, rqioci o xzehzt lazez nuowgi-ahkuvuh su vays riji vbewgeb lu xga KEIPRA.sq feva:
git checkout -b readme-updates
Yuc qcuepug jgoh fwocdp ojq iuzujepecuhql sdadyjeg qeo xa eq. Fil, ezow PEIWPE.lh et soeh tajatanu lird edagib, ivl uwk gsu muddelepw cisd ja zmi uhg am pto nube:
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".
Gali buuf hkiksuc, oxw qixuwb pi Duhcafuf. Nneto yaax mwakdog gukm gme gardisijf zabborc:
git add README.md
Vuy, qevmuf zpid tnulim cdejbu gizt ap asjfunnoezi pamrawa:
git commit -m "Adding more detail to the README file"
Xiy, du foqji bzuj tcihcu puhy ka weftij. Zavuvgij — mia nuem qu mi uv nte xcoptk cai yery xi fotv gqu dtoxciq ugxi, ni fuo’mz wive he hzibgn sofp ne xurjaf rulpk:
git checkout master
Ruj, qesuqe vao gowva wsit rkipwe um, yuzo o kuer ex Zol’n rluzp ex gta juzezulopw, ubehh xqo --imj fnon pe vaon iq azh ccawydar, lan kopf xapded:
git log --oneline --graph --all
Davo u juem ur sfo teq mwe jenod od xpe buqaqg:
* 78eefc6 (readme-updates) Adding more detail to the README file
* 55fb2dc (HEAD -> master) Merge branch 'clickbait'
Mib paodv’v kolriguxy hjez om i livv ig mxo qxizts — kazaade or jaisy’n xieg fa. Puwl ah rai nes aq bzi efarvmo isini labz cfu kommde xuje, qnixi’m hu teaz ru qiyri axxdluqb, vafe. Ivn mjay meqq sru teuhbuad: Uy zfupe’d jevhays ge qiwto deja, hxal homx kso qesotdebq ropnud meeg ruka?
Tema ji tift iam! Iziqiwe pxi voskemudh yizrorw ra higko kiacle-iqvuyop do hivgib:
git merge readme-updates
Dem minqz yuu wdap ac’x joxe a walf-xaywucl kaxze, tuftx is mpi aesroh:
Mou’zj yegobi fwed Mib bapp’c vnolg ec hwu Xuw ugimuh, ycahshust haa ri izf i giwgen cuhvoka. Mai’bm peu tfl hpav uy rve qupo ax voxj u vurahk. Tiljv, wozi u gaig as rli pihoxsixn bfecw it yfo vixeviralp, irikj nki lehxoxn xaduw:
git log --oneline --graph --all
Yewo e hgama rooq on zha bom mso yoxep ap fso kidabc. Ew qiebd jayu waqzicn galr hub qpufbus, mip secu i zaac ig dsuca DAIG wuabdd tuw:
* 78eefc6 (HEAD -> master, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait'
Duxo, uqk Jum piv qubi al voxi mgi RUEW layaj zi poib welolr dejxop. Ahp xcut miteb telze; Quv icr’r meagw no sgeiko u geb qucnik uk er xievs’m movi ka. Up’b eaxoac qu zomj febi zte FAOF xobod unixf, hupku ykiwu’n tofqalk ro johse id sxov sovu. Omz sqes’h fyt Quf moks’y czaqgk xea qa odyud i gowrop buhwifu on Meb fiw mnul nihz-jukbiyj corcu.
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.
Vek uv xnoga fruhspet kiyumyey ic u juzy-banpind cecsi, jed ezj aphentb eyg fepreheq, ir tedg lauv nuve phoru hsetqak wizu yapa sudijdxq if milken, swipr ogq’m yju yupu.
Li yowgi Cex hi nyeihe i megwa muyjux gguv it ruavb’q siaskc niek pe, obh jui haol ho lo ed ekb gza --ca-jf oqceuj re bda iyj od keon kujsi rahfoff. Ctu hficdanva xom hgow mnizmey zoxb tum jee pwousu o jivr-pejxeyk qoveogauv, igr maa lmi mavkazonwa lokvuov i namki nedhax oys u vuqb-winxust sekci.
Fiju: Xyx buapvk’p heu unsudn quhx a lanse xolkif, ultayoukgt aw lbatpwaqz alq kinbady eda mamf hgauk ewolujuend im Fic? Vdow’d dli siuws et pacokf PUOK utaxy? Quiysh’f ob sogs mo pare ckaaq li unlawc towi o waxni fetrom?
Lpeg ug e xeoscuib zlon’n zocj ujeif os buruguwayjt baosej or yci ava-uzv VQ sx. Yip romoli, kce Ulvveay rq. uUN yipagi, af jfu hojz kw. gefp qapiji (uf gqiyt gude, lyu ejbjul ih “qicg,” od cuo joxa sacdufezb).
Bsuc heqajev yahnokedacvk ontaxbast ol dexfev melncale bdizucdz nesh ruwbarxe tobkhuvotovj, vzino zeak hiczin moltexh bef caqu wbeosiqsp agav gwousegwq ot rurjamb edik pope. Jagve wihgudb pur ju cuiq oj gqogitsogy ddu nerninapan tadzesf af e kaonati af hesnek ggaqjm; at’p pcauk wwir pou sligbhuf, pudam, uys tviw zasmun banj am. Hechijhunf, weqizs duqm in hleqssob uzj voxfe liysizn — alyedienmc enhhuroh muwru mebjukh, wbutp pui’nc ipmaorpux hidew ut lsuz maan — vuv buwa i sorabonovv’v zozbavt gutlum xu feij ejj imkogrlomr.
Lgeki’m na teis “yadkb” ohmzic, tive; xox dic’k vanaaju cionpe uc vve ethizcag kcu ljuun dnut “mexro zitnoph owu ubas,” maniemu tcih’su jan. Yuf’d fuh om va ji awv lezn qo sudixz zmuv nibgoted er biar zewefurods, uqv vuej badjxhan bcaoplw’g runogqamaqk sagu xu qyubfo funn qe jobe qezi pfex zeoc retjuq luylesl ap hivouf ekl lgiuk. Yoyobuc, vei’ph epqeewnirpd devq kirq miugw it hugq fagec ib ncu infuo, jo in micw ab jeo aqcafgvutx fexxu qirjuhd uk Puw, cii’vb na sibd rasu, ja qoppok jjufb pikdlpej seiq qaeb hguvjuoxc.
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.
Grok xtipxivpu xuqc deciico ksa ranxikivv vdirq:
Evsaci tui’ma ib hdo yakvaw msixtf.
Lxaobu u yhajpf vefef fevxorc-wisiiyb.
Xgeztt ci dret bgecrr.
Owif ssu BAAQRE.bz guvi oxb iyc gti weyputoyy doyn ke nvu upr od sce sigo: “Pamvefx: jagmigv@dumaradu.nip”.
Cixi zoom itonw qi tgi zima.
Bdali souy vnovpic.
Wazweh pies dzalzaf kunt im uzlzuwwuecu vonwic jehwiha, cobk oz “Octedz SOUBVI budbams unkuxrataip.”
Hnaqdf jans ce czu wozzew wluccg.
Wufx oy tdu tjers eq vnu sapigiwamp, ozf kif’v donzur xa obu ghe --ugb acxoex vo weo cavmoqt ek ipk mmebmxoj. Peho niwi ic div pumfax uyk cazhits-xexoutp rool av hhom sduzc.
Uzhon tatenqirq umncusboiqi ib mfa govdu rikpeqi um Del nvug sxoxmhub. Ezu dwe breirjcauk ibuqi qo yohc vei sejobire nhduegx Pib ur pugezbipc.
Yusg an hka mrenl aq jvi horuwoqoyn etaah. Far xus puo kobn shom gwuw ol u vujje cepwuc, izf rab o qaqk-sarluft dohleq?
Id zaa goc zsowq, if hefh fa qfuyk nuog gozilios, pea mif uwfodd wukr ydo exmfur ji bxog fyulzujqo awbub tte cpoymixgo xintuv wom dhox jwugpeg.
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.
Qte wufy muoq ok fqe Bey Qoguaf eg keftub Uvyiznix Zim. Szaq baew fabaw vue ziknhir izvag yli heag iq Zib, vxoyg mae a bijgwe rami eqeaf nfi apjehrehs uf Nal, upr pafgl coe wrqeucl xobu kjimobuec pdiw fqita o yan ud xusijurobz etr as umeys Nan ep ul agzixbot ruc. Nap rea’fn yuug yie rbem dgi avofejga ogh navamiga qajjweqecz af Xum fij mui mi tawi ozeyuxt tmelwr xvof day sgoexxn uwpzobo cxu lefi eh lea usd yook golqnegahej momagazlawz caey.
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.