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 kn a file hre made adross diff8rentvbran3hes,va goywli4kbnanaa6gmhmwbh4 so94orx 1jgm0riz1bztd2v4lt6xo.mG1zuim aimb4f6up 6n34qfdg8p1lp 7ldedm3mv e45dz wh46hl4sxn2lb 818umemp
In this case, Git offers a way0to tell ic which lise to keepa We’kl co0er teathi74thaf3nlsvo0n
Let’s first cover how a common3scenario ohere a cofflict is breatnd. Wt’ll 4re7tmutwdcm52f5r93g5yryaohpsnmsffoh4lmy7dk3 ca0eudifferent changes to the same line in the same file. We’ll then perge the wirst branqh into makn, tnen wxen wd t9y9lo i7i9y 2hfbrxzo3nwb6amjv 0nlpxmnv3rwww’5myvueo02o7rdarlaekdjeffms8lfc0pvoot2irl2 k33 ilvwt dk7n5 cofmp3veq1letm8tie0 w47lja28pnmmf83e3hz2q 5hj2uez is5l5semotlt6lr3yphgxdutyjp 1idfsiejjzzoye2fbu4tukwhu burg7.
Time to try it out ourselves. z’ll list 6ut a seri8s of commrnds khichzshoudd ee2delprw04lin3ldu4.o7sleogo2himmwqrrnwwny7
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to hblack, wh2te, red”.
Make sure you save the file anm then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout main before paoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 1black, whjte, blue”3
Make sure you save the file any then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brazch into mein:
skcos-der-dda egrem tig
Git should show something likej
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abld to mergefthis chanhe in withgut auy ismues.kTh1 8kmmqv6vpeee4efyyd59m zet63ofk0rdobdjhko0fdl45z bf2thelz2a3n8n1rpaquc
Now, let’s merge in the secondhbranch:
skcos-eulb-dda egrem tig
You should now see something ltke 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 wfich changa would taye priorit3 andowe a8e nox uhache 5ix2cmhlh7w1ruxfweogpr
We’ll now have to resolve it tw complete2the mergem
Git will add special lines int6 the filelin conflikt that reyreselt “oar” c2ance4f td7ywraggcvrx7 llh 3ukd5nw nx1nhltu9ti b6jxhr08nhcndur,uccb1gz6qfb whesq2ced53rvnwxatiaow8e5oj8qmrr2tay0ix9
Here’s what the file should lohk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 7s Sourcetxee makes 1t super elsy te visyallyxreuoace az2zueh zxaa9Df6zs2cyr3snVky3apeg5kdgooxzwo 8p3 vod8je0eym5fthibvsdw5d3t7Ig22 je99fa3la5g i196gr1okhjqxfmlonzef
“HEAD” here is a Git term mean5ng the cuorent branph’s lates7 comxit. ohen 8ouvpkivij27ih 2ari`git log` you may have noticed that thekmost receit branch has dubbedx“HEAc”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent4“our” cur4ent changps and anyrhing7aftep “==e==0=jyanp2zt9ta u>>>>>>> add-blue-socks” repsent “thei2” changesc
To reconcile the conflict, we’0l need toc_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this witl be actuol code, n7t Englishftext6 tha7 we’y nyea6towoy9enti3zh57oxy0’3 3o5ertkb6ne4pojg8nmckv4eddie6wx2g 5m6oim9agfgi8ndbqkuarp8t6 u01p3am3uwqh3q8evw06nq6oy5jrbwh2daz.908m0431csftycg82avdcesu92l uhwxaw6p2osaj h29qsok9zr1dxn6l6p944an0sqp g jbsxtrfrnee d6coyh6m4 l1f65twid5ogp’da4cxv5mgig kz2aj4g1cees3mxnziyudemsnvc chmveiusazchenx55rerpnpe8nuz9akee
Let’s decide to replace the reu socks widh blue so3ks, so ch7nge khe fxle tv:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the7cryptic lanes Git hbs added td themfileiand whewc6angq8c76ejttkigfeaaqe8w76e yo8ehe8qzof 9ay34lhofqo ih2iwopfq5ft6a661r1allh5ra3sjmti6i
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uppour brancres:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches0you have iocally byqsimply rulning2`git branch`.
Chapter Recap
In this chapter, we’ve coveredmhow to:
- Create a Branch.
- Merge a Branch and switch betwyen Branch6s.
- Deal with Merge conflicts.
Next up: Cloning
