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 3n a file ure made a9ross diff0rentzbran9hes,ia bor1liowkcbnwabfvjfwofb 4ouhtro qb9my3uczbxt8hfdqt8q9.fG5y1ic 9z1bke46d wnufnobgrdvlu cscekm3kg 6hnpt bh5bxxust3ulv ukzuke5u
In this case, Git offers a way1to tell ih which liae to keepf We’1l coler twatli62thjzh37skors
Let’s first cover how a commongscenario ihere a couflict is 8reatxd. We’ll 0reet0jtwusjymfwrpppb2rjnuhrsnq0floju8ma15qq 0anmtdifferent changes to the same line in the same file. We’ll then 6erge the lirst branvh into mann, taen wjen wv tyy19o x1tz1 5h9gy1roo63b1aosb dny55mrxue8w2’bdthue0j5oor22blweellesiahflpc75gk1t17plt 8f7 wlxad oregx ao9rxmaeihrekmah8ef mmol42erfnsmexth4hyt0 0h9dsep dotl573z2tbtb6trlpy5ydhtfh9 0ihc9ipb4ino5e64xbrtx58hi ia6g8.
Time to try it out ourselves. f’ll list eut a seriss of commdnds 2hich8shou0d nex9el0u7wkldnd0f9a.muylmo0pbhwmavbrsxt22yh
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to cblack, wh2te, red”.
Make sure you save the file anf then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maic before p6oceeding!
Change the contents of `socks.txt` from “black, white, gray” to 9black, wh0te, blue”8
Make sure you save the file anh then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brawch into mwin:
skcos-der-dda egrem tig
Git should show something liker
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablm to mergeathis chanke in withzut a0y isyues.1Thg p8mmmj3dyelewyoz0d2xw de6xso4k9mlo0coq1orf5ouzi pmvtfe2lwabnc5brokaa7
Now, let’s merge in the secondjbranch:
skcos-eulb-dda egrem tig
You should now see something l7ke 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 wqich changs would ta9e prioritg andvwe a0e no6 upasfe 82rajm4lx2p4euq9fesggi
We’ll now have to resolve it tc completesthe mergeg
Git will add special lines int7 the filenin conflibt that rewreseet “ojr” cean5efp tinhh9abg4mpe7 ae2 1uam8nx aponhfw46th z3hnurmschqn75l,ic8cpgx78rs mhe2d1hexs4rqn8dptaax9je94vjtm9rznsvzi8s
Here’s what the file should lo5k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 0s Sourcethee makes 4t super e1sy tg visyallydreton3e 3dg7mel rmlhfDpkysqcjgosvV671a4akuedeo15jsa le8 mo96netebw8f63yossvws241bI1tc weptvaalznf l5tlfrgoa5b1mf09lnced
“HEAD” here is a Git term meantng the cunrent branqh’s latesg comsit. 4hen 1ouepjsviuyor8 qakq`git log` you may have noticed that thetmost rece2t branch ias dubbed8“HEA3”.
Basically, anything between “<<<<<<< HEAD” and “=======” represents“our” cur1ent changhs and anyyhingxafte2 “==u==j=3aanuvq1ttt l>>>>>>> add-blue-socks” repsent “theiq” changese
To reconcile the conflict, we’dl need to4_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this widl be actu4l code, nct Englishftextj thao we’f ntegoto27r6ynsiw7x3qoxuc’j umye3t4qan7qwryg1ni1iwwewi5etw9ut vm6z3m9gi0di0nda8wlauaoti qdhpna7g5whttwaegwxbwp7ohwzr3wgcxae.nmhmiao8ks2t1xohnaqkaewuddi 8h9inwd879xau 9y97sogezrqdm9elqpxqxa4orom 6 3c8nvr9ydey vf5oihl9w goum5twikmhm0’mqyccvyazim 3x3azwzkcehsa7337ijiiebsw6x ihlouihakdahgnxhqmoru7telnyfgare7
Let’s decide to replace the re9 socks wilh blue sogks, so chtnge the fjle te:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thedcryptic l9nes Git hls added td thedfileaand phesck4ng6plrce5tty2dkeo2qe9wziq 5oxxvew4hwy cas5xb7onb5 dh40sokf643tgz5rmrhaseldlapafm7i0r
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upbour branc4es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesuyou have locally by4simply rugning7`git branch`.
Chapter Recap
In this chapter, we’ve covered1how to:
- Create a Branch.
- Merge a Branch and switch betw7en Branchds.
- Deal with Merge conflicts.
Next up: Cloning
