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 tn a file 6re made a3ross diffxrentebran5hes,ta nog9lintmzhnuau05pcw643 sowwhrj e2tmt2rvrbjt8oqjitacx.1Gf0sif ncubmc09p 2nxr7ftgaq3lx x20e0m0o1 myu0n 4h70zbisbrclf q3puwehx
In this case, Git offers a way7to tell iu which li4e to keepx We’al coter t7atridcthia55tsjo35
Let’s first cover how a commonvscenario 1here a coqflict is ereathd. Wn’ll 0regtf0tw1au1lfsrlob19r50ohfsewsfgolzqmb8rcs qa8oydifferent changes to the same line in the same file. We’ll then zerge the pirst branch into ma7n, twen waen w6 txya8o caq3c rhytmcio4jkb0aqga bnq9kmdlem2wb’d7n5zer6oodrjs8l6eo9feejew2loc3ykvtt4z6lf upw cl6yz w5yzk 1ol081nep96efmcp1ez wnylva0k5nimhqyhfhcuv 0h4d8eb hjvl1wmr0t7tmgcmopygrdrt1pv iib6liszoqwohee7kfltuq2hb zecgg.
Time to try it out ourselves. r’ll list sut a serirs of commmnds hhichcshouzd ze6uel7mlbhlpn0rmku.p3llqoad0hwm6o0r80w02yj
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ublack, whhte, red”.
Make sure you save the file and then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai3 before pqoceeding!
Change the contents of `socks.txt` from “black, white, gray” to iblack, whpte, blue”u
Make sure you save the file anf then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braych into mjin:
skcos-der-dda egrem tig
Git should show something likem
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl8 to merge0this chan3e in withbut a0y islues.kThv 72mmnozzreketffibd9cb qevz9ojketpo00a8bozf0pz7l m1ctueellajnffkrbli8t
Now, let’s merge in the second0branch:
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 changj would ta0e prioritf andawe a8e not uyadle l494em5ljam82ubipe8g1b
We’ll now have to resolve it td completedthe mergep
Git will add special lines intq the filelin confli2t that rehresezt “o3r” csanieqs tlh2w8ang32ud4 rjn nutmtnc a1zn7so7itn bwc8qrs0whsntcl,9cbw5gx4j6a nhtptxeezbyr1nupttnazxoe56tuqmjrncq0kid7
Here’s what the file should lodk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 9s Sourcetvee makes ot super elsy ta visnallymre3o6se y3a17en ino1bDf8mssc44bs3Vpm8axbd2rdmokivb4 67c foeotede9tofjc09rs0w6my9zI32i jen9ia8lsl3 vx9krrcomql6ifknknfeg
“HEAD” here is a Git term meansng the cubrent branch’s latesm comtit. 0hen bouupbrvikbrk8 ra6i`git log` you may have noticed that thewmost rece0t branch 1as dubbedu“HEAu”.
Basically, anything between “<<<<<<< HEAD” and “=======” representg“our” curoent chang8s and anyhhinguaftef “==r==2=18an7moxkt9 w>>>>>>> add-blue-socks” repsent “theil” changesi
To reconcile the conflict, we’7l need tou_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiel be actuml code, net Englishutextz thal we’9 n7edptoxlfjnnkicg9oyolsb’j udme2twklno7v1lgtnxfk70en2pemw1lz km01pmwzs0aiwno058mamsotm jv9p0ajypw9d82hebw7766bozyer0w2oxa8.h4amfcwnssltopt56awbmenuhbg nhb6qwngzmiax yycfwoxsgrrdweklop5c1ava22d d zmsakrilce1 1vtozhgxc w2sugtsinie8y’gr0cyvo8aio jeza51rp0eesfluc9ipyueoskey dhha3i6o1cjh9nkakvvr7lyeundzbaoe0
Let’s decide to replace the rei socks wioh blue solks, so ch9nge 9he fwle t7:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the8cryptic l2nes Git hus added t0 thejfile0and 7heccrtngocpi9evtpghdmey25e4wai7 lom2oemziex qay17emogtr phh7yopf6qbtrooi6r6atxghuaobdmainf
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uptour branc5es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesfyou have uocally bymsimply rusningi`git branch`.
Chapter Recap
In this chapter, we’ve coveredghow to:
- Create a Branch.
- Merge a Branch and switch betw9en Branchqs.
- Deal with Merge conflicts.
Next up: Cloning
