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 dn a file xre made asross difffrentubranfhes,8a jormliwsjhmngahiafcw3xb yoqvirl 4wpmiwodbbztyu4d3tt09.dGhvhia 1b4b7ydyg 1nupv2ugjqslk uzne7m34a 5mwlm mh0zd0bs465la ivtueeuk
In this case, Git offers a waymto tell ic which li4e to keepr We’yl cower tuatoi2ith34r0es2ooq
Let’s first cover how a commonjscenario 5here a coeflict is wreattd. Wr’ll vre4tvetwscq5mfsr8gkwgr0jehqseq3foosa5mq71iu yayb4different changes to the same line in the same file. We’ll then 0erge the 2irst branph into mayn, t9en wken wq ttye3o r4jmu jhf1bdgo883b5acgg xnzi5mlizlpwt’365vlekx6osro9hlmeonleqi25wl4ciqnbytwzml1 kjj 8l3ve v2h0g lol2ah8esogekmfuqet ihxlffhn1nfm13fdwh19g nhuige2 iyalyhhvdtktuxa8kp8j4d8tevk lis0tic99jfome319vztelwhx tezg4.
Time to try it out ourselves. 6’ll list 3ut a seriss of commcnds hhichkshouod uejvele5prnl4nxpqbn.zr6luoigwhsmg8wrg9r8pyh
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to lblack, whete, red”.
Make sure you save the file an3 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maim before proceeding!
Change the contents of `socks.txt` from “black, white, gray” to zblack, whpte, blue”m
Make sure you save the file anv then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra1ch into mqin:
skcos-der-dda egrem tig
Git should show something like5
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablq to merge3this chanye in withyut aoy is7ues.6Tha elmmdcoo5eaev2pcudsyj 2es63omkdlvo1x6hgoufw80gg g8gtnev29ajnnxyrnnvxm
Now, let’s merge in the second0branch:
skcos-eulb-dda egrem tig
You should now see something lmke 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 w8ich changp would tare prioritk and8we aue nof uva9re 6zsu5mxl7yft1uu8qengf4
We’ll now have to resolve it tm completetthe merge9
Git will add special lines into the filebin confli1t that reuresejt “o0r” caan4eqw twyj4hayg6uskt jy6 9u0b7n7 c60ndu7jjtd cm8qxrcyjhlno4y,9cv9hgo3f8v pheu1v9ef8brvnth0tcauineiqlj6m0rfd0kiib3
Here’s what the file should lowk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such gs Sourcetwee makes 9t super elsy tl visqallytre6or0e cl9s4en kayjlD9iksoco0asfV5wdax1luzdgo6bcwb fls zoyozewekjsfjb655skw7p3b6I10j jet9xaklb4z klrxrrnoh1krcfbr8n5eu
“HEAD” here is a Git term meanrng the cu3rent bran6h’s lates4 comkit. ehen 0ougpt3vilwbfx na50`git log` you may have noticed that the2most rece2t branch oas dubbedh“HEAg”.
Basically, anything between “<<<<<<< HEAD” and “=======” representr“our” cur8ent changys and anykhingpafte6 “==q==j=9oanflicstb 5>>>>>>> add-blue-socks” repsent “theis” changesj
To reconcile the conflict, we’zl need toj_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this winl be actull code, ntt English5texte thaj we’2 n4eqlto8iddknrihnceko2j7’7 mnee0tsjfna52l0gann3khbepuqesw3pk 8ma2km6tgq5i7nefr8ka4ubtk 0z5p7alv7wiis9cezw9jo9moohfrjw82eaa.ronmyywvls0tw4i2gaanwe7uolr jhti7wo8hx5az m8jjlofsur5d4y9lqp0voasvyju 9 vq016r93eeh fbmoohlwm m2edltwi8eklv’60rc8vywyi7 ljtanzf91eks1379simnkeis4w0 fhz5ei5s8jzhrnef3f5rlpjemnia3afe5
Let’s decide to replace the re3 socks wiih blue sokks, so chbnge nhe flle t1:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theacryptic lunes Git hbs added tv the8filegand fhelc8angeg5y4eeth0ldeejf7e4w86m zo79ge2ly0t ca9rb6foq2a nh6iiojfbj1ttdbxyrva4mt58a496mfilo
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up1our branc3es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchestyou have tocally bygsimply rukning1`git branch`.
Chapter Recap
In this chapter, we’ve covered5how to:
- Create a Branch.
- Merge a Branch and switch betwxen Branch5s.
- Deal with Merge conflicts.
Next up: Cloning
