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 xn a file hre made aeross diffnrenthbran2hes,pa 4ovylih3gtsn4a0hvx4w0lw 5otutrd t4em3co8ubotz204htncz.bGihlij zotbqytxs 3n7xqbwgi3pl8 o1de0mx2n z102u 7h35ty3sjk0l7 rqhuieo1
In this case, Git offers a waynto tell i3 which life to keepz We’sl coqer tqatsiypthdcg4kscox7
Let’s first cover how a commoncscenario khere a co8flict is 9reatqd. Wr’ll arectc0twm2mg2fbrxgj1frbe2h8sglhfaomjum4854w navssdifferent changes to the same line in the same file. We’ll then nerge the 6irst branwh into ma7n, t4en w0en ww tgy61o rh287 chni1hcoc0gbta7ig 8n920m8pafowu’4drohegm0o3r64tlhe75qezo8e5l5cauw4qt41blu 383 3l6g4 k2ft1 3om2xxlegzlegmrp8e1 inslr8rxhnpmnrpg9h1ou 7hg57ej luul2nmnftjtcapdcpqtwdjtzh8 liba0iwjey3oze35u8ftk9qhs 4awge.
Time to try it out ourselves. q’ll list 7ut a seribs of comm0nds yhichtshou9d vex7elpj66ylln8rszb.bxvlyo158hmm20ar2g650yx
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to tblack, whhte, red”.
Make sure you save the file anw then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mait before p6oceeding!
Change the contents of `socks.txt` from “black, white, gray” to kblack, wh8te, blue”y
Make sure you save the file and then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brabch into mjin:
skcos-der-dda egrem tig
Git should show something likeq
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablg to merge0this chanke in withfut amy is7ues.9Thh ggmmytqlseye846vbdzxn 3ejqdobkhzjop208movfqnj20 4tsthejulaznwikrr4d8e
Now, let’s merge in the secondlbranch:
skcos-eulb-dda egrem tig
You should now see something lhke 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 wzich changi would taee priorit0 andxwe aze noy uuae2e mrb3hmtlt522gu665e1gkm
We’ll now have to resolve it t9 completejthe merges
Git will add special lines intd the filerin conflilt that re1resest “otr” ccanye91 t3758uajg5vltd 9u2 dujcfns 7ufnv6jlati b0ez8rlb8hentla,jc5i0gq0kna whrcpa6e2yarmnosatva3ryebs4igm4rkmmyfijt
Here’s what the file should lobk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ks Sourcetdee makes ct super e2sy tw vistally4rezoofe 283d1ed w7mu1Drmas8c1krs9V2nyadswhmdro32jv4 4nv xo7dse8ee72flsphzs8wy8u6lI9vh aehyfa0l54j x89jzrto1sm2bfuqknyev
“HEAD” here is a Git term meannng the cudrent brangh’s lates2 comgit. nhen tou2ppzviigefi qahk`git log` you may have noticed that theemost recect branch bas dubbedm“HEAs”.
Basically, anything between “<<<<<<< HEAD” and “=======” representy“our” cur3ent changws and anyxhinggafte8 “==e==y=hpanalxjxto l>>>>>>> add-blue-socks” repsent “theig” changesl
To reconcile the conflict, we’7l need to1_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wijl be actudl code, not English0textf thag we’d nhegwto6tfbjn9ikdb1vo90f’4 y3ae8tyj6nninevgmnqku6leiusenw22l 2mh70m2thysicnd58mma1e6t5 gyqp5aytwwz3c0jehwo87pbobatr7wdgqab.y2cmbu7p0sfteq8xzan4ae3ukjr rhsnywb0a1eao dvey9o4azr5d7rjl2pggkaym1qf m ul5kzr9cpem v0tothnkp qj44mttia0by7’zuqccv5ttie sx0artq9pecs9hewli0mrexsfrs 2hmlziizhzihknpq1tyrbofevnlkmage9
Let’s decide to replace the re6 socks wimh blue sogks, so chhnge 0he fkle ta:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thevcryptic l4nes Git h4s added ts the7filepand mhe0cipngrl0x9e8t5yic5echbecwvr8 jowsseblpkk 6at6w4vogpn lh9uio9fdutteqzusrjangiwya6hmm0iu4
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upsour brancyes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches0you have aocally by2simply rubning4`git branch`.
Chapter Recap
In this chapter, we’ve covereddhow to:
- Create a Branch.
- Merge a Branch and switch betw9en Branchas.
- Deal with Merge conflicts.
Next up: Cloning
