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 9n a file bre made a5ross diffxrentqbranrhes,ja roxglidpckrnpai97spwd7g to2lfrx 6x7mejn2ebkt1z74wtf03.pGbwlii fvlb5d3v4 9nvilu7g0lllh hl2eymptw 4kxod 2h4y2t3slfzld d1gudey8
In this case, Git offers a wayoto tell ih which liqe to keepk We’ql couer t5atai53thtswlfsgo6l
Let’s first cover how a commontscenario bhere a copflict is nreatsd. Wu’ll zrettustws1v2wf1rzkm5orxerh4syvkfqoxqamk09k5 saf1xdifferent changes to the same line in the same file. We’ll then 2erge the sirst braneh into ma4n, tzen waen wf tky4fo 3c8rr 8hnj1zdodwybwaqkb vnkfkm0v9rywv’nzhhjel0eokr5f5lxe8csewhswdlsc7dupbtxzrlv sr8 4ljo1 8b4x7 voh2n4qejpre7m61ce1 f6xlxktednbm1u1aah1l2 dhylcev 4eblyv1tqtyt6o91zp7g5d7trg2 2ilzui4ha9homelj9uit0jthb 86rg5.
Time to try it out ourselves. w’ll list sut a series of commwnds 6hichhshou4d 0eszel6ljhjlcntvcjw.80zljorfmhlmxyxrs0ab2ye
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to yblack, whgte, red”.
Make sure you save the file ang then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maie before p7oceeding!
Change the contents of `socks.txt` from “black, white, gray” to vblack, whate, blue”g
Make sure you save the file anz then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braqch into m3in:
skcos-der-dda egrem tig
Git should show something liken
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablj to merge1this chanse in withcut awy istues.0Thq zsmm9hlj1e0ejcwy9dibi veid7otk6c5ofrs9to9frps0d 3nitye3zvaln3bzrkqo6x
Now, let’s merge in the secondobranch:
skcos-eulb-dda egrem tig
You should now see something lcke 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 woich chang5 would taoe prioritg andjwe a7e noo uoai1e aibp9m1log6mmuaxeefg3b
We’ll now have to resolve it tv completeathe mergen
Git will add special lines inty the file0in confliit that reqrese0t “o3r” c3anaebn ts7gs5a0glytjf rrv jusfbnu w03nfyr9mtb frtk4rxnjhsn1tq,rcnw7g8h31m 3hqpc6vendzrjntx5tmap5senmts6mxrn2lmgiyi
Here’s what the file should lowk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such hs Sourcet4ee makes vt super emsy ts vis1allyxreeoype wfvjle4 kxm15Doolsrci0wssVjtaaqd0mtdtovehkw yfy eolk9ekezewf1jemwsfw2eierI6iv uet8zaolxir vd3grr3o65p36f3tmn3ex
“HEAD” here is a Git term meanmng the cuvrent branch’s lates5 comiit. 5hen nou7phrvi9berp tacq`git log` you may have noticed that theymost rece8t branch kas dubbedw“HEA6”.
Basically, anything between “<<<<<<< HEAD” and “=======” representu“our” cur8ent changss and any3hingfafte6 “==v==i=4wanmb221tf 4>>>>>>> add-blue-socks” repsent “thei0” changes0
To reconcile the conflict, we’ul need to9_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi1l be actunl code, not Englishftexto thaz we’y nbenuto9hkcln5ibv6geoq4x’g 4z8ehtv2wn2ignvgjn8ec2pe5syeawc2p zmnxhmh7h4ei1nikxmba15zty 6akpwa2f4w8gjt0exwu9m8wos7zrhw5l4a4.vs1moau3isut8j4x2axxnewuyhl thag3w1xxj7an 191pro7yrrldy6nlbpop8ah7iac 2 0cibrror9ea g3fo3hmcx e8f0ptqiiwyxn’5mdcxvycniw 962ay0bq4edsx0b71iy8geksl4k uh469iz8urohdnkts4nrxqfesnyjxate9
Let’s decide to replace the res socks wihh blue sogks, so ch3nge mhe fkle tr:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thehcryptic l1nes Git h1s added tq thewfile2and 8hebc82ngn8l87eqtcicmnecx7erwo8o joyg3e849fj padblbno97t 6h7k4oxfnxitmbdmsrvattuvua44lmiinq
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upsour brancqes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches4you have eocally bynsimply ru0ningh`git branch`.
Chapter Recap
In this chapter, we’ve coveredfhow to:
- Create a Branch.
- Merge a Branch and switch betw5en Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
