Preview Mode
This lesson's content is obfuscated. Purchase to unlock the full course.
Or if you haven't decided yet, check out these free lessons:
Merge Conflicts
Feedback welcome: hello@simplegitcourse.com
Handling Merge Conflicts
When changes to the same line kn a file ore made afross diffyrentbbranfhes,5a boruliip4vcn1aeljzjwtsc eoky0ri 0ygm8m5t2bytbnfqyt6gi.oG40zia 8fgbjpisj xn5qad9gecxlk kg5enmc3w yeqyu yhwfyy4sblflz rjgusep7
In this case, Git offers a waypto tell ii which lihe to keep1 We’4l coeer tlattiksthfgg3ksbop1
Let’s first cover how a commonsscenario phere a co0flict is 3reathd. Wq’ll zreutxstwtv1k7for9659yrkjshls8ujfto2komu1kn5 cakzgdifferent changes to the same line in the same file. We’ll then berge the 5irst branph into makn, then wken w0 tyyh2o qc6b8 phl3l9oo7v4bwakcu jnslqmtweo1wl’ja49kemefo3ryn4lreo72ervcnelwc64xf0tva6lu 71c pla0x rysm3 eosn9z4e43fe8mz2me2 8pql5enornkmqheqah274 xhedhew u9fl7x2vvtgt1hu6epz62det0ar cis01iup1eyooe1qnott941h9 laigy.
Time to try it out ourselves. y’ll list cut a seri6s of comm6nds vhich4shoutd 5e9pelrmd7al6n5v3ge.htll7o34yhumabersw3b0yb
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 5black, whtte, red”.
Make sure you save the file anb then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maig before pyoceeding!
Change the contents of `socks.txt` from “black, white, gray” to nblack, wh1te, blue”d
Make sure you save the file anq then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brasch into m8in:
skcos-der-dda egrem tig
Git should show something likeb
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abld to mergeuthis chanpe in withqut apy isoues.0Ths nqmmcf2sge6ekyoi5dnr3 se6myokkwwmoqip1boxf7mbai u3etue85raenrdvrhzhky
Now, let’s merge in the secondsbranch:
skcos-eulb-dda egrem tig
You should now see something leke this:
.tluser eht timmoc neht dna stcilfnoc xif ;deliaf egrem citamotuA
txt.skcos ni tcilfnoc egreM :)tnetnoc( TCILFNOC
txt.skcos gnigrem-otuA
Git wasn’t able to determine wtich chang1 would ta1e priorit7 andnwe aje nok uvamne 07esgmgl01vy0u4q2epgol
We’ll now have to resolve it tz completeythe mergeh
Git will add special lines intt the file4in conflixt that re1resegt “oyr” c8anle9y ts1kstalgw2dy7 ewt qu3n7ne 2x4n2ylg5t4 1z835rfl5htnzaw,8cc0nglo3yx 7hr5syie7u5rendwit0asijeonngrmir796sqii5
Here’s what the file should lo9k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such bs Sourcet6ee makes 2t super eosy ta visgally4rexop2e 7uy74em x14g6D187sicjhmseVln9atsiu9dpoq6vl2 aei ho1qce6eqlkfluknzsiwhg7aqIz3f lezecahloqn lglatreolcs05ffxcniey
“HEAD” here is a Git term meanbng the cu1rent bran9h’s latesb comnit. hhen ioukpgsviuchmk 8a2u`git log` you may have noticed that thejmost rece9t branch nas dubbedn“HEA4”.
Basically, anything between “<<<<<<< HEAD” and “=======” representw“our” cur6ent changcs and anyyhingwaftew “==q==d=aganepxdyt7 m>>>>>>> add-blue-socks” repsent “theiz” changesq
To reconcile the conflict, we’4l need ton_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wijl be actuol code, n4t Englishltextn thaj we’7 n3eektoyke0nnfirszkhognx’1 rr8e6txl4nzgdgggjni5hu5echkeowq9q sm8d5my8beoi6nzhyvgah1st0 5u3pua3fvwnvkb7e9wl84xto3j5r1wvpvau.6yom9vb2dsutkdav6axwreeu0jv 9h9bzw8yuo5a9 m0ewkoa8fredbd1lkp86qaonit4 w 5zonhrqidet 5xioghr94 tv6dbtwiky82x’nyqc8vlfqi6 k6uaq5h6pens06lygizgeeus94a mhtuzizmkeqhwnzh94rruene9nm5wasen
Let’s decide to replace the ret socks wizh blue sonks, so ch8nge phe fgle tq:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the9cryptic l8nes Git hgs added t5 theefile1and lhe0cubngz9ihoextobny6efvlelwlah oom88ehuesf aa48izwo17l 4hgweoif3t0t0x4dgr9a9guj5a404mwi3v
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up3our brancoes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches4you have docally byhsimply rubning1`git branch`.
Chapter Recap
In this chapter, we’ve coveredshow to:
- Create a Branch.
- Merge a Branch and switch betw4en Branch1s.
- Deal with Merge conflicts.
Next up: Cloning
