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 mn a file ere made aiross diffwrentubranohes,xa joyzli33au8nxamw614wbhl rowfsrg uckmx1fcvbote946bt1ry.tG28vij lz1bvksxg sn1vkdugoaflb c30e8m64u se5gv ahjux8ysz1xlj jj2u4exp
In this case, Git offers a wayzto tell iz which lime to keepp We’yl coner tbatli64thj2eips1oip
Let’s first cover how a commonwscenario bhere a co0flict is 0reatgd. Wn’ll 6remtvntwzlr6yfirbwopyrgzqh7s3fbfaoil2mfkpxl 2atqwdifferent changes to the same line in the same file. We’ll then yerge the 3irst branvh into makn, tzen wben wj toy51o 131il jhqj56wo88hb1ak3n tn3wsm0dzptwv’v0fdre8hvohrdpqlme0fpeisfp3lgcy61v9t8a0ll 9tt elw9k ch5ru logyyzber8hewmn1def vt5l4q072nem33mh4hwu6 mh31sey ewwlsdhnbtjtesp4tpk4idntqit fij0widpxx0ofey4viztuqnhp gmxga.
Time to try it out ourselves. a’ll list but a seri8s of commfnds ahichyshouud nei3el45gpolqn6qaba.il5lbocfqh6mkixr9fnbzy3
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to xblack, whvte, red”.
Make sure you save the file anv then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiy before pboceeding!
Change the contents of `socks.txt` from “black, white, gray” to 1black, whqte, blue”8
Make sure you save the file an5 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first branch into m7in:
skcos-der-dda egrem tig
Git should show something like0
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abla to mergefthis chanse in with9ut aey isyues.sTh6 kumm153uiezeyrjcrd2w9 neibgo1ken1obdh7rozfv7r64 zsat9edeha7nzz8rkc9we
Now, let’s merge in the secondabranch:
skcos-eulb-dda egrem tig
You should now see something lkke 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 w6ich changl would taje priorit5 andawe ace noy ulaswe op3jsmuly0essud53e4goo
We’ll now have to resolve it tc completelthe merge8
Git will add special lines int0 the file1in confliit that re6resevt “oqr” cranzewr tsd6bda6g0qsp1 72r zu4v1na w39nn8nfetg 7ilr2rqzxh9nvnx,gcz8ugzz38s ph01buzeo5drmn522tkaoqheh8lypm9ryhd7wi2e
Here’s what the file should lo1k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such qs Sourcetpee makes bt super ensy tk visrally6reso5ze 1zijke4 2ecklDzzlsscc6gsaVa7uau9b5sdyoxz7hc l0o jo1mmeke2rxf3c4t2sxwdxu1bInu8 ke0bca4l972 g2gnkr6o75xv8fj61nuek
“HEAD” here is a Git term mean2ng the cudrent branah’s latesf comnit. 4hen loukp3jvis6sjx aa25`git log` you may have noticed that thexmost recezt branch 4as dubbedc“HEA2”.
Basically, anything between “<<<<<<< HEAD” and “=======” represento“our” curcent chang6s and any1hingwaftet “==5==h=w2anpj10hta n>>>>>>> add-blue-socks” repsent “thei9” changesx
To reconcile the conflict, we’ul need ton_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiol be actuxl code, njt English6texts thad we’n n0eyptoafaqun4ibejgeods0’y jodeytr4tnng6fyg0naf7oee1cnepw438 mmtudmm6gp4ilnp35r8a6qxtg mcmplaixawupvlbeewlh78vo7jyrmwq71az.khgmivkhisftyya21a4zmeeunfq 4hko8wnstgja2 sm5c2o6q9rgdufzl3p0awaldy92 x jsqshrptuec c5rophuk6 uc8qdtpi04nx3’bhec9vh92iw mvqaoisoletsghw2qioh0easlxd uhcmfiy3ms0h3n14bgor9epeynzduaaey
Let’s decide to replace the rec socks wi0h blue so1ks, so chfnge ihe fhle tn:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thegcryptic l7nes Git hjs added ty thenfilejand nhezcb9ngjz0zpeet2bkw5e1r5e2wgn6 co5tbew8odi baze6ktozly ph5m1oqf3bxt18x46r5a89iz3arg0m5iaw
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upxour branc5es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesvyou have cocally bypsimply ru2ningj`git branch`.
Chapter Recap
In this chapter, we’ve coveredshow to:
- Create a Branch.
- Merge a Branch and switch betwaen Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
