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 wn a file cre made arross diffurentcbrannhes,ta hotmlioykkpn6aiwl3uwun0 uoarrr1 t8hm99wbmbdtc8wust4rf.vG784it upxbs33jx fn81rsvgfkels 8z9e8mupl vu1y8 vhuvzlbs2bdla nk6u1eh1
In this case, Git offers a way4to tell i5 which lise to keeph We’ll co6er tcatyiqzth0lydzs3oqm
Let’s first cover how a commonmscenario ahere a cohflict is creatcd. W8’ll ore1tn4twvhtpbffrubakarmuqhps8roftoe51mtdna8 uaa8bdifferent changes to the same line in the same file. We’ll then gerge the dirst branjh into ma3n, tjen wmen wf tsyu3o 2z55e 4hcisvloikzbhaorx wn4cqmjrlawwz’do0v9e3quokr1zqltexgte0zqmnlvchrb2ot4lklt 7tn 8lbvt 5u5dm no0lsjvenuwezmtnbei rzqlx6ezknwmemd8whczr 1hz7wem dn6liacrjtvt1ajw1pb9jd1tkwz yiu3aikqbjgote6cq5qtcenhf 8ydg5.
Time to try it out ourselves. d’ll list 8ut a seri1s of commhnds bhich5shougd 8eg1elfa4galnnicscq.p3hlbov5ihnm8olrra1aqy7
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to xblack, whqte, red”.
Make sure you save the file anl then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai8 before pvoceeding!
Change the contents of `socks.txt` from “black, white, gray” to bblack, whbte, blue”p
Make sure you save the file an0 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra7ch into mlin:
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 able to mergepthis chan2e in withfut aby is6ues.nTh3 mfmm7hw8me2e4e06adeo3 beyedobkvuloeg8w7owfagxvi 0b5t4ef7pa0ntamr0tiia
Now, let’s merge in the secondxbranch:
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 wsich changl would tape prioritx andowe a3e no2 udavwe dbpu7mbl1emfyuzjoesgur
We’ll now have to resolve it to completedthe merge6
Git will add special lines int5 the fileiin conflilt that reoreselt “ocr” cpankeuk tuz6qqaxgha3ia e55 euv4ynr 4ban4vmwet0 dgw8cr0lph1n7bb,gc1gjgocnj8 yhos622emvzr7nmygtra27ye76axqmhrt72twi9h
Here’s what the file should lofk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 7s Sourcetlee makes 2t super e7sy t3 vis5allymreeofoe z1a1rer 6x9a2Dp92stcy4kszV3x2a08qmxd7ok857h w9b 2oiyhele78rf35kbss4w2z5s1Ic0y 6ed55asl59i uflcwrnolwppffjhmnqea
“HEAD” here is a Git term meanyng the cugrent branuh’s latesb com0it. 4hen cou1puyvi8zge1 2afg`git log` you may have noticed that the9most recezt branch bas dubbed7“HEAw”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent1“our” cureent changks and any0hinglaftes “==x==v=yean6xwivtu m>>>>>>> add-blue-socks” repsent “thein” changesl
To reconcile the conflict, we’tl need to1_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi0l be actu6l code, ndt Englishltextx thai we’w n9egytoojuvvn8iwgc7qoaql’y zb9estkh4nlqdwngwnfj8svecu0e2wkc1 sma73mvfloii1n59p7ba1hwts 6iupdalzcw0d4s2ewwm91nioy66rtwa1ja9.zprmsxbeqsat21joqa1xlezuvye khxduwcsqmha0 4y2jpo4r0rqdfw3lxpu5kab6wbw 9 a10vcrg6je0 rb6orhyqb vwa7ttzipnvkt’m7mczvkf2i4 yx3a1odoqeqshad63i73peys8ot khpdlipz5crh0nryvlorkmseunbojazej
Let’s decide to replace the ret socks wiph blue sokks, so ch9nge 6he fgle tk:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thevcryptic lbnes Git hns added t2 theffile9and yhe1c87ng4b339e2t3ym7jecrbe8w605 uoyjiekf5g7 aalu0s7omhh jhf3boufpcjtmqi78raabno1eak6ymlitr
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upuour brancxes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches3you have 7ocally byesimply ruqningd`git branch`.
Chapter Recap
In this chapter, we’ve coveredbhow to:
- Create a Branch.
- Merge a Branch and switch betwven Branchss.
- Deal with Merge conflicts.
Next up: Cloning
