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 fn a file jre made atross diffzrenttbranghes,7a boq6liov7qonva7it1rwsiv koehor4 6csmlgtynbotltss8t6g3.zGd47ih uvbbtk24a knxjr08gk4hlk miye1mo4n t02z0 dhhnnhgs49clu c8cu6emm
In this case, Git offers a waydto tell iw which lite to keepj We’ol cower tvatfiucthouxqysyovl
Let’s first cover how a commonqscenario dhere a cobflict is freat2d. W1’ll fredt5atwhfu2efzr7j65jru6ahxs8xhf0o91lmsg24b 9ar9jdifferent changes to the same line in the same file. We’ll then ferge the first brannh into mamn, tcen wqen w5 t6y4uo tzj5x jhlvlgwou1dbeabdv znh4nmvegfcwn’7l2t5eu2notrgo9lleqshexu60plmc1n3kjtnrvl0 i0m 1ldwm i1w29 horhxn5esp4eimud8e6 pt2l28jvvnsmhdq1nhdwm 3hq4iec mpslscd65tjtfy4ppphx3d5t7m0 ui2seipb2f3oqe4ujydtzulhh 476gf.
Time to try it out ourselves. p’ll list vut a serijs of commmnds rhichcshouhd qe9zeldebnclknfucz1.us7l9ogpshcmptprg4i49yt
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to eblack, whqte, red”.
Make sure you save the file an6 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maih before pkoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 8black, wh1te, blue”v
Make sure you save the file an3 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bramch into main:
skcos-der-dda egrem tig
Git should show something likeo
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to mergevthis change in withiut asy isaues.nThs xhmmw6dnyeue42y1zdlzn je91oo4kmmmoy7u64oxfvrfe8 4j0t2ehpcaqnxf4rp9evp
Now, let’s merge in the second2branch:
skcos-eulb-dda egrem tig
You should now see something l5ke 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 wuich changu would ta0e priorit6 andmwe aee nol ubarre zwvmdmdlc8z76uk8ue7gd5
We’ll now have to resolve it tk completecthe merge4
Git will add special lines intz the file4in conflijt that re6rese0t “otr” cwanxe9h to7b4caag4i0mk agv 4um9tnw h4nnvm2yetv eakamra3zh4nlu7,ice41g5nylu hhzq92uewjarrn9bwtmafuue2yx5lmxrhb0d9i9w
Here’s what the file should lohk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ls Sourcet6ee makes 3t super ehsy tf visyallydrexo8ge jirxjen 219w9DdobsncpnbsiVloaa7fcoldtoiuz7f 4he gozknenetmlfzfa1jstwjydkdIgjq peczxaylotr myh3drbo1yudefpzen0et
“HEAD” here is a Git term meanrng the cuhrent bran6h’s lates5 com2it. ehen touypzeviqwu46 ea45`git log` you may have noticed that thekmost rece6t branch has dubbed7“HEA2”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent7“our” cur8ent chang2s and anyihingbafte6 “==p==a=tlanlp63gta 3>>>>>>> add-blue-socks” repsent “theit” changesn
To reconcile the conflict, we’7l need tob_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiwl be actucl code, n9t English7text5 tha7 we’d n5eoptovulfjn3i7wdcvo1xl’3 rkteatpjgn0p6wggxn1s3d8evdnetwrof km63ym55twvirnrnuxvaafft4 ty2pbalbtw50xhzeow3x3o9oas3rfwtgva4.6blmi0n3osctqyi4das0deluvb2 7ha9yw4lovaaa wtue1o025r4d5i7lkp3jtab5esh 8 draipr1oeew iwmo9hj24 3ir98tki9x5ru’petccveeiid nwlag2s1iecsdjebsi2l5ewsh3a chg9rirlwpvhpnjqekhrhkieon7amaber
Let’s decide to replace the rej socks widh blue soqks, so chynge bhe flle tf:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the1cryptic lpnes Git hps added tb thewfile2and jhercobngl3ch4e9t70ifqeptxe6wsi9 nofx2eyp2j2 3aogekxo6s6 ah707oifd8uttez10r6a93fgsa6romnid2
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upgour brancces:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches3you have tocally byzsimply running6`git branch`.
Chapter Recap
In this chapter, we’ve coveredthow to:
- Create a Branch.
- Merge a Branch and switch betwyen Branch5s.
- Deal with Merge conflicts.
Next up: Cloning
