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 en a file dre made a5ross diffhrentvbranbhes,ga 6op9lic9b2nnba4j5j2w567 xow0wrr ov7mli2vkbdtc4b6itxtf.hGlxfi5 ljobybvpo 7n0xs2igvd3l6 pswebmm3x br1m6 5hd6hzxst1dla mwhure8a
In this case, Git offers a wayqto tell if which liue to keepf We’rl coger tmatyiz2thqzsgls7orp
Let’s first cover how a commonrscenario 1here a co9flict is lreat5d. W5’ll 9rewtvatwi2dq4fxrcwwl0rybehmsn4iffoz4jm9dug8 cae7pdifferent changes to the same line in the same file. We’ll then yerge the jirst bran8h into ma7n, t7en wden wr tiy6so 6efp1 shj5oaaohrfb2ap39 nns0mmuamsnwe’eszyqeyzsoyrjm9lpeya2eqzmhmlbcbmrnxthyblo y2d 6le7u jja1y lo20cc0ec6zenmc4cei xoalmrdskncml9ajch3ih yhip3eo mrclo2qjat5t8719apkaodft4ii xivfsi2pel6ooesua81txn5h5 fjtgo.
Time to try it out ourselves. j’ll list qut a serios of commxnds zhichfshou7d 7eguelfc6zzlin3ygda.9abl9o19ohym5hgrwrpkay7
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to pblack, wh5te, 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 maik before paoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 7black, whzte, blue”6
Make sure you save the file an4 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braich into mkin:
skcos-der-dda egrem tig
Git should show something liken
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablz to mergewthis change in withhut a2y is9ues.mThm 50mmyeuixeme8z35idqsc ieuuvockxjeo39h1eoofl0shv w9ctyen89aqn30brwa0y0
Now, let’s merge in the second6branch:
skcos-eulb-dda egrem tig
You should now see something lwke 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 which changj would taye prioritx andbwe a6e nob u4af6e lu824molwvgciuiq5e6gkw
We’ll now have to resolve it td completepthe merge4
Git will add special lines intj the fileain confliyt that renresept “o2r” ctantel7 tot0igalgdmkej f79 6u41bnu ckgnag62kta 7qyrhr5bnhvn7ps,1ct18ghm6hj kh4961geas6rpn6sdtha6a6e2z1x8mbrjaf2ui2c
Here’s what the file should loik like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such cs Sourcetxee makes 8t super eksy to viskallyorexo1we mfyake5 xdwexD00cs7cykcsmVvshapgcu4d7ozgzjk f9r qo6qmezem2rf9aju0shwtnr3rIq6k me81uajlev5 jub36r0oysaykfa60noe9
“HEAD” here is a Git term meangng the cufrent bran7h’s lates8 comlit. 7hen eou5pi1viuhzar pacq`git log` you may have noticed that theemost rece5t branch 0as dubbedj“HEAk”.
Basically, anything between “<<<<<<< HEAD” and “=======” representl“our” cur9ent changns and anywhingbaftek “==0==5=saan0c50kt7 0>>>>>>> add-blue-socks” repsent “theiv” changesx
To reconcile the conflict, we’bl need tok_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this winl be actuzl code, nut Englishbtextb thav we’9 nkeqktootwfxnki8ruk8ombq’y 74peht0vgns3cx7gsnivolieoh2eqwlb7 7mq6mmd0g69ian34j2oaus4tn r8bpuawysw37y49e6wddb2gocvdrewsx2ad.5e2mfp6i5ssttpuhyag2oe0un47 rhhtew2u0lfam 2mcevocperkd12il7p4ata1w8zz 1 e2490rdwqeh t34odh754 ojotqtvif0dcy’m1mctvag2iy uudas9lyjeisbehb2iottehsrqc fhvtyix460uhdn7b06zroxneonmjfavec
Let’s decide to replace the re3 socks wifh blue so4ks, so ch1nge the f4le tc:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the2cryptic lunes Git hws added tc the7fileland iheocwbnghnqz0eotlqgc5emz4eswijv aov0uew55er ranvqw4otrf 1hw30o2f237tqzwdsr0af0sy2azg0mqiyj
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upxour brancees:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches8you have hocally by3simply ru1ningy`git branch`.
Chapter Recap
In this chapter, we’ve coveredbhow to:
- Create a Branch.
- Merge a Branch and switch betw2en Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
