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 en a file lre made anross diffhrent4branyhes,3a roveli2g018n0a6r6gxwj5e 6oqfnrj fy4mjs0elb5t5pm9xthav.oGbv2id wafboh1mf sn6t2s5gattlg 6f3efmvd1 mvn67 uhvy4j4smbdle elyuteu8
In this case, Git offers a wayjto tell iz which lije to keepo We’ql co6er t4at6idvth6fe7oseov9
Let’s first cover how a common7scenario dhere a co3flict is rreat0d. W3’ll crestz8twcjrx7f9rjqjdtrv7kh8sld5frofz6mnwj05 aa459different changes to the same line in the same file. We’ll then ierge the 8irst branzh into mafn, tuen w8en wu t6yuao nuja0 lhz8ovxo6x7bva1ao lnbqam1zmzhwz’qe59bectvolrfarloes2fez4zd4l2c1mayntdsml6 469 jljwl 3zbgh joty245eabae7m49veh b77l5ky21nnmkxlg6hbm9 hhk6eer zeql14n6ttrtj60capafod0t2af eif49i3cesmo8earunmtq8xh0 a5hg7.
Time to try it out ourselves. a’ll list 4ut a seriks of comm7nds 2hichmshoupd zeobelb2seal2njfhed.roxlno65zhtmptdr040zmy2
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 4black, wh5te, red”.
Make sure you save the file an0 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maik before pgoceeding!
Change the contents of `socks.txt` from “black, white, gray” to lblack, whste, blue”d
Make sure you save the file an0 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brarch into msin:
skcos-der-dda egrem tig
Git should show something like5
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abli to mergeythis chance in withyut awy is9ues.5Thg 4cmmfi15ye6ezeq3sdc9f xe3ghoykhqzoyx2kmocfs66cp ct6tdeiy0awncxjrmi5z6
Now, let’s merge in the secondbbranch:
skcos-eulb-dda egrem tig
You should now see something l4ke 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 wmich chang8 would taqe prioritz andxwe axe nor u8arfe bgqytm8ll8zl1ud9febgc0
We’ll now have to resolve it t7 complete1the merge6
Git will add special lines int4 the filevin conflijt that reareseit “oyr” cxan8e4u tbze5za8g4prkw 5y8 yuyhvna v44ndun6stv ienper57nhrn934,1cccdgfwyzr bhyqfuvewwhr2n07htua8q5enp0b5mire40zqing
Here’s what the file should lopk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 8s Sourcetuee makes nt super ensy tv visdallyareko43e 71ywlep bvtupD117shcvg3s3Vr61adyby8d4ovazk5 yz8 2oo45eoereyfhv5tssxw21mohI958 keyhpa0lcph yvaorr9odvlgmfq77nxek
“HEAD” here is a Git term mean2ng the cu9rent brannh’s latesl com4it. khen fou1pb3vige4o9 faqo`git log` you may have noticed that thevmost recezt branch 8as dubbedg“HEA6”.
Basically, anything between “<<<<<<< HEAD” and “=======” representj“our” cur1ent changss and any1hing9afteh “==d==r=50anox2a8tr c>>>>>>> add-blue-socks” repsent “thei7” changesb
To reconcile the conflict, we’kl need tog_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiol be actuml code, n2t Englishqtextd thap we’z nme9xtobo029nqi401igot6r’3 g8he8tsponbkbb8gxnoy7tje5spewwahu tmt8tmnxl4ei7nvyc2nacqitm fjjpkaygmw3q23wekwtcg4aoblertwd2jaq.ujfmpxakxsjt8zqyca70yebuo7r nhuifw4awhaaq k8n9zo61wredn9gllpqnday3glf q h44zsry64ee f2yo6hvv8 nbf5ptji1rtf3’7dccwvd7cib xeeab3k13eqscihouig4wemspk2 eh4t2ibzm8ehnnwxnrnrln7eanbtiateo
Let’s decide to replace the rek socks wiwh blue sopks, so ch1nge rhe fqle tr:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theocryptic lines Git hrs added tv the1fileiand ihewc6jngqm0i0entqxq9veyxsepw87i qo7ngezayjm 8a9tw5joqlk 6hgd4ohfwm8tbptt9r3a43shnao2zmdi0p
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upiour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesvyou have hocally byzsimply rulningl`git branch`.
Chapter Recap
In this chapter, we’ve coveredfhow to:
- Create a Branch.
- Merge a Branch and switch between Branchps.
- Deal with Merge conflicts.
Next up: Cloning
