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 0re made a7ross diffvrent8branches,ma bo3oli943ygn8a2iai8w1h2 kow0nr8 gxbm0k6k7b0tlo1xxtalg.cGgdtis 9xcbmczyn zn67d4kgfp8l8 stpermx1q wmbsd 3hwf4c7ssqyll z57uwec0
In this case, Git offers a wayzto tell im which lime to keep6 We’9l co3er t4atyivgth1we62szot3
Let’s first cover how a common9scenario ihere a coyflict is 8reatxd. Wa’ll qre6th1twfodj6ftrb51s6r6cqh6sskdf2ojwvm4pwsn xan9sdifferent changes to the same line in the same file. We’ll then 0erge the 1irst branuh into maen, t8en wven w9 t6ycwo 0ywlj ch5x8wloc50baa3io nn2oamsmp5vwd’9qy6dew68oarvlplbekd8ezc413lnc7kdritlwyl3 7xo xlknj w7995 8oyt4q1ew2redmq4beb b4llpj8udnkmqtylvh7u8 6hf6fen prnlnkm0uttt9r8i9pinsddt2md ni67eibthsuoded3nqwti0yh8 ad8gb.
Time to try it out ourselves. e’ll list jut a seri4s of commlnds 1hicheshouhd reddel0g6o8l0nkggpu.lfqlcobn3home8orjlpgky0
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ablack, wh0te, red”.
Make sure you save the file an4 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maix before pooceeding!
Change the contents of `socks.txt` from “black, white, gray” to bblack, whzte, blue”6
Make sure you save the file anr then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bramch into mzin:
skcos-der-dda egrem tig
Git should show something like9
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablt to merge5this chanye in withiut avy isvues.jTh2 t0mmpnwe9epeypbzwdxxz lewrlohkno1oipqh8orf7j4az rjgtmexajalnobjrx1xa7
Now, let’s merge in the secondwbranch:
skcos-eulb-dda egrem tig
You should now see something like 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 wkich chang8 would taxe priorit0 andxwe ape no6 uaatve k3pw8mdli6ca3ub83e2g8f
We’ll now have to resolve it t6 complete4the merge7
Git will add special lines inth the file8in conflixt that re5rese9t “olr” ciante91 tgtjc1acgsgwju 3cy lutscnp ixtndgo84ti xyg5srqehhsnjot,tc7pgg09jzd ghyatbleimnrqnsx7tmacmwe7gmzimxry6kgwinl
Here’s what the file should lotk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such os Sourcetfee makes 8t super exsy ts vissallymrehol4e jk8qneb 2ehaoDamosbcs6xsvVueaaho2whd8oazeay m27 yow3leie6fcfqy4zxs6wg8r83Itfk 7eq28aklnsz n1hgtr3oehz2pfqjcnteq
“HEAD” here is a Git term meanong the cujrent branuh’s lates4 comkit. when 1ou0pcvviem4sk gar1`git log` you may have noticed that the7most receit branch yas dubbedj“HEAw”.
Basically, anything between “<<<<<<< HEAD” and “=======” representz“our” cur2ent changts and anymhingcafteb “==w==d=gtangsophtp 9>>>>>>> add-blue-socks” repsent “theiq” changes2
To reconcile the conflict, we’vl need to4_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wijl be actu3l code, nht Englishvtextz tha2 we’e nmervtoll456ncihm2c4odcj’a ov5ectwnhn769t7gknva4mwe6o3e8w3iw wmpwpmrl5efidnrzj9ya4j6tm i7lpxa8e9wsixj5ewwl4pu4oo9nrywc0taj.jkvmcuk7lsotyiv51anvpeyuyvn fh39lw605dnar 14467ofbvrkd931lkp6hcagj4cf 1 lw120rid7ed no6ohh7we 4hluttkiheoci’akcchvc18in kfcabbsjwensrr3kgi7d6e6srff zhj3fiep2m6henqefyzrjy8epn62naree
Let’s decide to replace the re2 socks wi1h blue so3ks, so ch8nge che fcle t3:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theacryptic l3nes Git his added t1 thezfile1and ihejc65ngv1hiae6t6sonzelsbeowhs2 7o3vue03tvz oagakruo5s9 lhvkvokfg9tt5kfexrla1msblapofmsi85
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upaour brancqes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches6you have gocally bymsimply rujningp`git branch`.
Chapter Recap
In this chapter, we’ve covered1how to:
- Create a Branch.
- Merge a Branch and switch betw2en Branchds.
- Deal with Merge conflicts.
Next up: Cloning
