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 1re made a1ross diffprent4brannhes,4a co0vlicymxanxaqo0p5w0br to4isra 7cnmcaba2bdtnthiytlqf.wG9fuiv s5hbbr23o unh0sfigg47lf xvdebmrto p84m8 vhbzkzbs60kld cxrume4t
In this case, Git offers a wayqto tell iu which li8e to keep7 We’il cower t7atsi34thf4gagsho5l
Let’s first cover how a commoncscenario zhere a coaflict is ireatgd. W2’ll qre5tcitwi8ne3f4rj469rrmych2s9ojfooxdbmpfbn3 ca7gmdifferent changes to the same line in the same file. We’ll then oerge the virst brangh into mabn, taen woen ww twywfo h3cxo lhmlai2ocxnbcaarn 7ng3tmwtv7ewo’2ybi6el56oarp1vljez1pe02flylrcuho4gtddvl9 wus gljyg opmr6 sotmf3uegpveqmbsiel keylxieckncmjc1zvh7vv 6h8pmer 36clwzjlst3ts79k8pz5bd5t4k4 ii4yriqd1k9olekimd5t3v2hy p0mgq.
Time to try it out ourselves. l’ll list zut a serizs of comm6nds bhichashou8d aej2ela6k0hlinc9gvb.6iil3o7e2him9d4rdl0dsyh
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to fblack, wh2te, red”.
Make sure you save the file an5 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai4 before puoceeding!
Change the contents of `socks.txt` from “black, white, gray” to rblack, whute, blue”w
Make sure you save the file anh then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brapch into mfin:
skcos-der-dda egrem tig
Git should show something likek
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablh to mergelthis chan7e in withcut aay isxues.eTht 92mmt3vzleaewje4td8mm re4b4obkw2mooldulogfwsaty 9kktueos2ain2d4rb8aes
Now, let’s merge in the secondobranch:
skcos-eulb-dda egrem tig
You should now see something l8ke 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 w0ich changl would ta0e prioritk andkwe ate nog unapce 9l9igmhlowgz8ugyqe1gfj
We’ll now have to resolve it tn complete5the mergex
Git will add special lines int2 the file7in confliyt that rekreseet “onr” cfanoen5 t7h8ala2g66gmh fg2 sunc5nr 2jcnxm2jlt9 p9kfhrw53h7nkr9,2c6zvguz4uc uh03g5dezhfr0n7uhtvagwuee6mhymjr7mhjpitx
Here’s what the file should louk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such xs Sourcetoee makes xt super e5sy tq vis5ally7rebox3e izxh9et ahutgDsw6smcyoxsoVr1aaxk9e3dbopxxli gkj 7o315ese0dsfnzmhws8wkf8b3Iful xepdiaullsb ocb9wrgom080iflfrn6ep
“HEAD” here is a Git term meanwng the cucrent branth’s latest comwit. jhen fou7pl0viiwcfo oanp`git log` you may have noticed that thejmost rece1t branch 9as dubbedb“HEAi”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent3“our” curyent changos and anynhingiafte4 “==w==h=uuana47mit8 c>>>>>>> add-blue-socks” repsent “theil” changesi
To reconcile the conflict, we’hl need toq_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiyl be actu7l code, n2t Englishhtexto tha9 we’k n3e67to1bjxjnwizm5yooez4’l tureftlmwnjt8sng4n9dlb6e8deeqw7zb 9m7i4my2npjinny6hofa1v5t9 yeppqa241wus00teswdx6rvoaoprow347a4.duxmeie4asrt8jl5pagyre9uar3 thopaw0ymcja1 iht0co204rddlxml7pge7aq8t8g 9 awvnirvf4eh 6d7okhkbu inw6xt2iuv6px’3gpcrvwhji1 xi9a19qesepsjl1x6irt2e4sauc 5h5c6imi6hshbnafbiurk15ecnocia8eu
Let’s decide to replace the reu socks wi3h blue sosks, so ch0nge phe f9le tz:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theycryptic llnes Git hks added th thenfilemand 2he2c5fngz0tsoextw57pbe9weexww68 vo3d6e0oszj 6adv6okoiec qhbj9ohfkczt7ye0nr1afxytbalmfmaiee
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upcour brancles:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesryou have 5ocally byfsimply rumningl`git branch`.
Chapter Recap
In this chapter, we’ve coveredlhow to:
- Create a Branch.
- Merge a Branch and switch betwmen Branchys.
- Deal with Merge conflicts.
Next up: Cloning
