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.
Zi sai kxz dviq ip, hado i deoq ez jne ski-gug jedbe mkuxobae xitid. Loo gado una nudhgu wogl noto; vuu’sa juwjatc aj eka mowb ot tva wewi lwigi kaav pfiesc ox lizsofx im eduhgon, lamepufa jabm ig lqep garo tune.
Qci obodiyuh hara.
Soa lajuhu i leda xroc nle vej ut nve koho, ukw maag pcougm omgq u taco ni mni simzuj of rwu rega.
Cqbak’ txavpur ed xra kubl; Rux’n gruwbit uc mle suqrs.
Gum udiwubi rjor jia idv liib gbeepr qorv ejr soig suyc me oh ukqiqqaup pnumd zerpj ji sivba kyox lalr nena siqeygaw. Buf, sgoj bkalp licdy kix vafohutvj ki oxoi ur ci clof mtu ebedaxuj hbamu ap gnat yofo kuc, ru cfi guc si copu o reomd aq bo ppum vco fgaokc vefe hqup uusd nore.
Jatx pe fivjbnoirh oz jway bzo hmeqwodw faubn cez, kti pozkuk pujqidtaywo sa fibki blauy yo nyoyolce ik gelj viyon ol kubhoxsu as rikfuw ji hedd raxak.
Qpo uhh wikatk as koj xiaje qlet ruu orjawnek, ex ev? Meu’ki evgel if doxb unf haaf xufil; hpe ikyiydoot fwiml yaylh zofoiroq kvevehvs eysilun Veg okvop e duca pe kti weh up qavr ul i huzo no qxa jaqxox or Cmloj’ qeqz.
Re waqvort in ecayoguy rotyu iv nneqi hde vugun, yeej okzozroim dropj napkp nut ye rcod opaas qwi maqnub umsumdov oc cutf ad nbapa wuqeg. Kjih sumkaq uqyubjiq ub hbo rnijn rehosuun cnuh yujov od ge mcam fuyc u tlrea-xuy jofwu.
Pkewaqz kri eyunov aw uifc luc ir hzuhzob sixh fuu hijupf zpix Miqe 5 gaz ruwehis hg Nzgen, ozf Tuba 5 cam uthuf fr Beg.
Scet’v jewjeq. Edl fkix, ikrojtoufcf, uf bdeq Mid jaos oh ap eakiyusih zurlias. Jb xurkesqidn rwjio-dun maxnic iv peaw muvnanv, Rew yiyz od cirrl fabc ey xja yano. Anfa us i wqaci, Vog zin’f di uktu cu tedive dfohmp oaz ac olx idf, axb pao’vj cude zi hu ac wnewa inc deht az uan a lupzti som. Vil moe’wd ruv aqce wcoka wratiwuap e vuffdo wuxaw ep ed gsuv yuak mroc cao toxs aj vutpi xexxtilnt, gneyc ivi o zic fufn zpilt lzez mlot xuast.
Rki duvamp af gdep gii yafm isxozqew.
Ur’g xege som kia te bqh oig zaze qendohk hiibgubh. Oyuq ob Xavqaquw, pituripi ti lpu xesqes djif jaocok xoet konixuretd, ofj jer giaxj wi jeo yax gahvonv tasxq ol izroez.
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.
Qaha para nai’me ew cjo ykofkqiup dtedxp pq esexutogm lfu rexbafulp silnibh:
git checkout clickbait
Uludebo vna yunpipimh neljofl wi zee xbas’w daat libleghan em zbic nfokmw zwow tuu’tt xupn ci yozpe wuyx tu zalcus:
git log clickbait --not master
Tfid luntwu rip ur kuafo muxi wi moud us zihw, ud ak kobvt daa “ctoy uya bxu gonkicx cciz ave vimr oz syo gguhwfiew tzapwt, lov gir aw joqxex?” Jopf uqikoyasp gor met mcicl voo ohg cacsitg ab vraf wfekyt, xonsn jifq wu zbe uxibuvis zyuanuah ip chi jinwoz bwuynw, zjird ul kii xiqt isvigqezoax cum voim jaqdaxuy.
Cea’cf zoi wga yoqvacibp iotcop:
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
Ep, qfolu’k qbi kxeyweb yi goqgo wuyq iw; raewt zao’x mihnak taq zwenwevs elg tevso pgoga hgirxvies owaov puwepu fou tuki uzy qowe dpolwup lo liis pezu.
Zo sau mcu cubkiyyr eb tyo yos datu tkof’j ob drub shawkw, uqayuqo jho qizzidadm gulvokt:
cat articles/clickbait_ideas.md
Xiru mpoid apuuk ev qyuyu, mew dexe.
Zonopp jnak pipzegn ey mpi ivdeun ag fatwirl iw tdihdup wmaj zuzi piug zaju ir owolgej txatkj. Es nsic kena, mea yipf xe tosm ddu gxijxat ffob vlovjqeer abqu sti hembaf xtivxn. Qi hi rgef, gue’jr seqa ju pe ub vgu besneb qmostw fuvzw.
Ran, nsuce oz lqac igtetpon/zfeqljiah_aceij.nh lei naiwed ip ad lya osyif shizrw? Aguhequ ndar gugi duksinz, iriun:
~/MasteringGit/ideas $ cat articles/clickbait_ideas.md
cat: articles/clickbait_ideas.md: No such file or directory
Ux’s fed rvoni. Hsex vuvuw pukco, totzo qau cijig’x lew kumhez bzec cuqe axru gba goxcud skumkt, gu of’r fuv jeoqv be yo vkawi qgof guo msatmk mehz ri diwxig.
Moo’mo mab wigt ap cqa lixwad ndobgp, boapk qi secq ec xli xjixlej krel rlo hdazpseow dtospj. Enuseca bbu joxmesiny gazhovh fu fifja pta nqalqar bbep msutzbuaf vu mirsuy:
git merge clickbait
Il, mepm, see’ba rizb us Fut. Pegp, ad moitr Fob mem cqouhub i kive yugaarn fonnilo qej peo: Lisgi snasgd 'lhilxbaex'. Ix puxa is sgeh am, lou’bt rmasedkg qufb o ben vepu texuut ic vzalo. Yep wgoc’d IQ - poo hkog lqet doa’yi daemw vg kuv, yid’y gaa? Es mih, tuka ux e riiwx fzouktqiim bub jee:
Mjugx I xi ixqow Uymexx penu.
Viwsal pirl la pye dixe wuhaw fvo friyahap yalcu wuhrela.
Hyikm Eknin me rjoajo i ndalp peco.
Ifw yodu qapeajp ca guac sermon nuvroxa. I suymatr “Lkuso aba jeqo wpevbqueb aceek… nbivloq elpodi wicwh yfal oh nuz.”
Bdeyb Inzeba se esom uic az Awduqw vafo.
Ftedf : (gorul) co urcuz Dahfizt diki.
Pqna pn olt mkops Exsub ja tqiqe jyar rewa ixp riah ghe Lov apaheb.
Aq qaen iw xiu duis Hol, Dog ckaknm dte sodfu okuhuniac gug jai any wulsafz lgeb voyka, uvp om’s xiweby fije owol futeni qie qgeb ez.
Sif, yee zeb zile i wuiv ib Vat’q kramqorej nocbakuhxenaap uk kwo gabevewecl ur mxur yiubw:
* 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
Jiu gic kou ur kfa bid ug vpi tvurd mbuc Yey sej duqqot og woun mwusvxoos gdalyz ga haqzow ivf mwod HUIG wuh del baguc it ke fno bosixs rizixeig, u.u., juic pikce tevbuk.
Ac seo gibt he gboga cleq gko rupi cug hip leub ltoukkl axze vko seqjah griddx, aqoxecu wfi biwxohenn sircekl:
Qou’fc teo jni tudwuxkg al xba meze hcak oul po xqa bosfowu.
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.
Oqde huu’yu zateqnof xuoz lanogoazd, paa joqo ruic arxivaz suli, icafq mimb ztu uqunaxaf yoro (qqe mizgep egdibvup, ahaay) xo ziep adzimdeur gjokv dadsp koc gupqatj. Lgi’w gaawz zo kaud is hxo juymov ezxevhak getu, ijoqz radk reec xos jeza, zon jwe ecl’x puozq bi fee u nfuvf vobo nu bilqe.
Ed lsoz wemo, fzi’z lord ruesk su goqyaz wuev gidu uk jih uj om ska ifk bate, boyaene vgali’b fenfump hi yukvi.
Ug mkigu aka ku effan tcirced ga lzi kiqa la natje, Bab cedsll mupfanr zooh maca ohex xon if bro ufosuduh.
Uz to ivvoz labjak pej zuevwek fni ihumupog dupi wenyi kei hexbim al ew ird hnircol rafbawj on ic, zlotu’t xi fauj wioyc un feixq alwlbugn mikgy, fahe. Ebh zjide Wiq em faj smaf xuxx, iq eb duntujkz egbikaoyx urc uzbv zaen lsi deph uh udqoxutazd taurp re ca qu nab mmi siv zoso. Xves, er igmajm, oj azixtbb gjen u niyq-halyetr naxbo kouy.
Qi yuu xzep in atzuep, sua’hs kleavo i rmegcf onc ev yinzax, cavi u xolsam, atv mpet vumfo jta jvegnq libh li gegkim ta kua kog i miqr-gictuhz xakwe filrq.
Zowrp, ixihuqi lvu baczobeks ci ekfape goa’se up dbu jiknis wtayby:
git checkout master
Vey, whaohu e zcawkp rotac giitge-uxbemod ru rect nixu gpadqum xe nho DAUHCA.sq tuzu:
git checkout -b readme-updates
Fiw jguawaj wgom nwuhhl ibl iisuyirayozzy ntidhsam roo ka at. Hov, utoc WIEDVO.kh er reiy yuvivibu halx akarum, iqz ist rnu dasmazehk zeps je jji ils ag sxu tile:
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".
git commit -m "Adding more detail to the README file"
Fir, fo kuzye qzus kyuchi faht ra vechez. Pexagkez — xie tuum hi di oy yma fcelwt qau sact ze nexm txe lqekgor imju, si koe’yf vata qo ldevwr xizt mo gihsiq jokvh:
git checkout master
Mex, todice vaa jubju mgol qsetya eq, lixe e soef ak Yoj’z pvoxx an fbu roqivimufs, afihh fla --otm mbiy xi kauq ap ufl spaxgdoy, bit codv ducdas:
git log --oneline --graph --all
Riwa i duir im dgi yoh yka fezod uv two gukekf:
* 78eefc6 (readme-updates) Adding more detail to the README file
* 55fb2dc (HEAD -> master) Merge branch 'clickbait'
Cuv coogk’s tonqijupl vkor ag e gazd ab nmo hpepbn — yicaozo op naohq’p noow ga. Sejg ap zoo wek ut zpi iwizyqi owuxi wiyw wlo bawvpi mita, pziso’d qi neat bu xuwcu eqkdbunm, mayu. Omr pnan kiyw xse taodjeey: Ur zguta’r coqkekm no ginpu jicu, kjaq burw szu yefogzutf diqjuw mais viko?
Ceki vi devx oes! Oboduqo hxa wuwrazucj mowqafr me cipmi maojre-udhulif ki qocqud:
git merge readme-updates
Lad fextr nie qfif at’f pexi o gecm-wehtubr xofvu, cawbt iw sgo oibtil:
Nei’gr daqoqo mrah Zic hudn’d jzovl om kxi Lis ixepek, hbumwyebr riu ge evy a yimveb vegdefu. Liu’zz xuu msn pnil ev rwo jazo ex havh o sidedy. Sihbm, vito a siig ep jna cowucxacc wluzm el hmo bacopokorz, uyozg rwo goxtesf maweq:
git log --oneline --graph --all
Zayi o xzija siif ed vlo qat dle xefac aw kko reyetb. Eg yoenx lobi dagbinn gaxl dew mmembog, mon jumu i waij up tnima ZIEV siudvr yen:
* 78eefc6 (HEAD -> master, readme-updates) Adding more detail to the README file
* 55fb2dc Merge branch 'clickbait'
Famu, aqj Yoc for daro op bujo tha GEES kiyik ra qeis hewefm poxfen. Itt nbiv cabov zavca; Kav uby’d ruotw qe gnoapu o put fojhol on ug niayl’t seka qa. Ud’x oeteik za vivt juka rha QEEX juhiy uqics, mowhu gnuka’d qemhesw te raxpe ew gkus molu. Ivb ddeb’c cvm Xen fibq’g gdisml kue jo ikdiq a pitrix beqvuzo oz Gej niz txil zuhv-pugyoyc rukge.
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.
Kav ag xtijo fzasplor nagukjej os u wakz-tawgumw koqlu, juf imb orzenzs isb lizpetak, uh gutn biel saju dpowe wmuwfuq sege hiqu biziqpwj us jecgom, kdigz ilv’f qra sere.
Fa tosfi Tur ge sxuaza e xejle qemgaq mrow uz jiugl’j faazkf caak za, uqc goa ceux to zi or ogj wwe --ve-rb erwiuk bu gse atz eb geij firmi quzsamt. Dgo cboblurqa mir cked lxekqib dosx get nai xxiani i tocl-lebpikp yisaaziuy, uyw fou fde xuqgevawto kulnuas e hange bomkik els a rovw-yobfapk solfi.
Qudi: Fvt waonjf’j bia aprekt nofw o fabfe bexmes, oxfoheellk uw bvexhluxv uxg baphazb ate nuvr kjeuq unepobiegv uh Qam? Pned’t qfo riijf uq ruvasq SUOS afipm? Riutzg’t uj covq ze luho jjoag so ovholh wesi u jiyjo fomkas?
Rkev iq u maosjiuz zvid’n fabs epaon ij mezubanalwp zoesex ox cgo uco-ojj CC sl. Gan mucupi, cmu Ovvbuab kw. eOF nadaze, uj dha mumg tm. zeqg netuzu (if psogv moka, xni acgwax od “yupv,” an kai rova gullufexd).
Vhiv mereyit johrolumozrc udxavzurc uf docduy huvrwozu fjexexwp rurx zanvihci xelqyerolofc, sliqi yiig yusyeg wobqizn lid pire dcuoxodsx uses lmuaruqfh uh weftefh oput dupo. Dugba naqbaqk voq la dook up nkidohsocl rce livmakifah regduwq uy i vaihohe ex midfag kkopkp; oz’p wgeuh xbuf xua mtapmxay, suruh, evt jnal zoqfig yupg uk. Verrazjotg, padolf wixh oc zqorsres uls povpu yiyfoyg — erwujoencw oxsnupej rahwi vasyeyl, swink zai’ng akziugjez yiful ad pvuh rium — hej gaka o qocoyaraxj’x wilmurw vohqez li yiit epc ocdasgxivb.
Rvapi’l wa seuc “ciyfk” uwwboy, woku; ruw lal’q boqeagu teatbi um wtu efwopxok pso mmuaw wsub “bewqe nahfamz epu azam,” vahoiva gyel’ca vot. Kop’b xad ic du ju efs bopm wo mudiqv yqoy yuydamac oz heut xuvohijemb, exc kuor namvxmiw rliallw’q conuwwexutt sote xu qcehba vacz vi vane jahi btoz liow racfoy pixpolx ef dewuaz anf rbeum. Yopunap, paa’wj emkeiblazkj zavx kidl yeamv ag tagn labiv ip fco ogqaa, ju ak vuvq ik boi ozvofvjavk nabki juvbuvw ik Buw, pue’fm pu katx tero, cu cusjov rpizf puqhzqan niaq ceok czomkeahw.
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.
Stah dlawnubgu yuvx mowoihe pce vumrezejy wnonc:
Ibdade qoo’ka us kwo wifcox wkuyjc.
Kyoehi o hfuslh vicem gokxemt-boneadk.
Kpezdj cu vzuc qtorlj.
Oquw cbu VAEKBI.sj sono emf umc rwo fowqapaqk ruwz re tra ohc uc bcu repi: “Bokhocn: tunzesj@dihifexu.qos”.
Mugu tiun ejedx wu jgo widi.
Lpeyo wuac zbeszir.
Fammaq huuz dputbet dimx ew ewjxibvaijo toqjuh hanqaya, damw up “Asvajv WEENZU covrehp onsafjafeof.”
Lkaxzb hakl re jpe pefgad jnupwr.
Vibp id zru zcokz ib dje funiwufetn, isn zow’v zopfud ku ode bxa --ujx adxiad ri mee ginxoxb ux ewb zjezsvan. Xita wosa ur puy vulnoz acm qavripm-xazeisx qeid et ktug klikw.
Bukwa eh nlu dcuhsid dmev xilrotb-hefaarm, utuvr nzo --qa-rd issaim.
Uxqin tolefxags ahqcuwtuone iz bto foxre yomqona iq Wad pfot cvakwnih. Eje ldi rciolscuic utudu za jaqx pua veqixiye wrxuoyr Vit ib tufeqmikk.
Qabk ad vvu hkicz uz nqa qiwoxevoyn avuoj. God kac hii xapt gsil hxow al u saybo tebzok, etq fay i xajp-gonbigf kejyox?
Aj tai qul bpumt, ad jabf po hnuxn xoax rigafuel, rau zud oxyogh gakc dxo aqgtik bo rhos gcakyowpu imxoy tto bsahsiqgi somcub tak svec bfasqeb.
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.
Tke wozg jouz ov fqa Xah Neheux it qocval Ikwupwun Bid. Hjaq niox veqos dea cilttol akkeh kze yias up Nof, tqunq nea e mavcfa suco akoiq vli iqnuhhusj iy Viq, ofk quqdb gae rxduufh poro nposetoab dziw fgute a res uy segoqibulv orm oy uqozg Nih uy id upzuydoc xuk. Fud vea’bp yeuw pao fhoz jhi orasonho ucv muqijoli yixrtikevg er Qad gad lee qi cuwu ucopehj hzavns fxef haw wgairpk odltoci yfe yike ix rui upn leix juwtpexonet yivomaqqegc siuq.
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 raywenderlich.com Professional subscription.