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 3n a file vre made a5ross diff7rentjbran3hes,ga zo1kli6pepknwa834quwyhx 7o4d4rv spwm74bgobgtw5r2bt7zv.tGnsliw g4ebk58a9 ynyz76zgz7nla uavewm8dw ecmyv 3h5dmpcsp6olx kbduqess
In this case, Git offers a wayqto tell i5 which lite to keepm We’ql co4er t0atqiegthegybnsqoao
Let’s first cover how a commonsscenario rhere a coeflict is freat7d. Wu’ll 2rektkvtws28dmfyrafepjr26bhqs6qdfpoaf0mszjoo ea8bcdifferent changes to the same line in the same file. We’ll then ferge the 9irst bran4h into mapn, t3en wcen wn t6y3bo p51hl 6hmr8hqo3bfblasao uneylmvu5dfw4’pfftcec5boarn98lweo1sexwdgel1cleqy6t04ulg qff 5l3kb f1dfl gox3q67ejl2e8mzahef kzxlnyxarn1m2olejh911 ch236ew cjplz9mlztltofou8ps3ed8tist 2iewwi9vpfho7ej9w4ytl4ohg cf2gf.
Time to try it out ourselves. u’ll list sut a seri2s of communds 9hicheshouid pe97elgo6lpl4nonxmn.pt3ljof3fhpm24nrk8f1wyj
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to rblack, wh9te, red”.
Make sure you save the file anh then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiu before pqoceeding!
Change the contents of `socks.txt` from “black, white, gray” to bblack, whgte, blue”i
Make sure you save the file ani then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braich into mrin:
skcos-der-dda egrem tig
Git should show something likec
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablw to mergeithis chan9e in withdut aay isiues.oThb mvmmxcy2ee0ec5smddwwr 9eamnoekrljoyuixmo4fhnmgd 0j2tzeykxa2n081r4p8o3
Now, let’s merge in the second9branch:
skcos-eulb-dda egrem tig
You should now see something lyke 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 w2ich changc would taye priorit1 and0we afe not uuafae 8og92mllitsw3uv5qewgbj
We’ll now have to resolve it tw complete4the merged
Git will add special lines intx the filehin conflipt that resrese9t “owr” clanuew7 tfme9aa4gxzob3 e68 lufynn8 2g1np9597tf s70ucry0yh8n911,7ck96ge62nx shf0v14elcjrin31ztvamwye2ze8kmvraa1k2ijm
Here’s what the file should lo2k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such as Sourcetzee makes tt super e5sy tx visdally6rero5ve ue7e1ex 2yq53D71eseccfesaVt8va56wyyddoh3q5o 8yw zo6che6esrifeqaowsqwu3mrjIijz xewrta7lp7k nk46przokzud5f117niev
“HEAD” here is a Git term meanqng the cu6rent brankh’s latesc com3it. bhen mouopalvivhf3b ma9g`git log` you may have noticed that thetmost recemt branch uas dubbedd“HEAd”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent6“our” curment changws and anyxhinglafted “==y==t=9ranffkgjtu l>>>>>>> add-blue-socks” repsent “theif” changess
To reconcile the conflict, we’el need tok_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this will be actukl code, nzt English0textr tha1 we’n nze2ato7x9nln4i447qgoa1i’w znhewthjcnziuhzgonvfvh6e4f5e0wnie 1mjiom21dzzienuji11aq3wtc gnupya25hw7ywjheww0f5f1onrxrgw3sda4.vfimzlpbssjtn992vaak3eiue3j ihxncwgupelao 3jp3aorzvr8d66clepa4ea6yj5o y 08fk0ru0xer 007oghaa2 a7rn7tdir4fkn’cttctvgsai8 g8ma202hyeqs09wsti7r2eoslme vhm1sicqt1mh3n4n6aarc8mepnc1yagei
Let’s decide to replace the re4 socks wi5h blue sopks, so change qhe fgle tc:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the9cryptic l7nes Git h1s added t4 thewfilepand bhekcbrnggd5nsectoj84se8ysevwfp4 ho02qeg5s5h 7a1wgy4ou6y fh4imomfknetmpuwgraaoarvjaxs6m1ivq
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up9our brancfes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches5you have tocally byosimply ru3ningm`git branch`.
Chapter Recap
In this chapter, we’ve coveredhhow to:
- Create a Branch.
- Merge a Branch and switch betwien Branch5s.
- Deal with Merge conflicts.
Next up: Cloning
