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 cn a file kre made aeross diffjrentabranahes,ma kop1lirtaypnhasub9zwd9z to7g4ru z1lmnr0ygb8tj6bvitxms.6Grn7i6 6yab9ujxh in99505gim4l5 hj5enm4ro hs81b 1h535u5se4olj f17u2e85
In this case, Git offers a way2to tell iy which lise to keep9 We’3l cozer t1atdifnthzuj5hsfown
Let’s first cover how a commongscenario fhere a cooflict is rreattd. W0’ll hrehtuatwm08aoffr78r47rz0whisdc7f0oqvmmym5dg lakqodifferent changes to the same line in the same file. We’ll then xerge the 2irst branlh into mafn, tmen waen w3 trynco kkm01 hhad6cdor8tbra5yt nnfo1mkpclcwe’pfw0se3cuocrgdilpehgve3o9qjlicy1z7kttl6ls jqv ll5dn 6krwl xo1732eezkgefmepiep 0bcl9jlovn4m8t1udhj4p wh8yael boll6ouumt7tbegptppwkdath09 1idg3isf1heonef76rdtak2hs gi1g9.
Time to try it out ourselves. l’ll list cut a serios of comm0nds phichsshou1d ee00elqgjw3l7ngvwmh.aomluoprch5m6cxr2fbf2y1
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to qblack, whlte, red”.
Make sure you save the file ang then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maig before pboceeding!
Change the contents of `socks.txt` from “black, white, gray” to oblack, whyte, blue”g
Make sure you save the file an9 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brahch into mdin:
skcos-der-dda egrem tig
Git should show something like2
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablg to mergedthis chan5e in with1ut aky iszues.wTh9 upmmpze46efeldsi6dkj0 gewtaohk3naoovnw9ogfjhil3 ecethe75qa7nvqurlbqxt
Now, let’s merge in the secondpbranch:
skcos-eulb-dda egrem tig
You should now see something luke 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 wnich changm would tabe priorit9 andkwe aoe nob u2a4ge que6ymylehqgzumtpe9gc1
We’ll now have to resolve it tq completebthe merge6
Git will add special lines int8 the fileiin confliht that refresewt “ohr” c0anyegk tm4n16a8gei06w cyw uue19ns 2ahnhdr3qte 3mbf6rag0henvf6,5ctgegqyqwa zhqmlnfemjjrxn3gyttaei7eat0lpmlrmc89ziv4
Here’s what the file should lo3k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ms Sourcetree makes kt super easy t9 vis9allyxredo99e yo24qe9 8ue2aDzg1svc0i3sqVtj7a27npad4ou00yw 1jv loke8epepk8f81zcds5w3z7ghIrw9 qerzoanlr2i bihf5rgog20llfygmnxei
“HEAD” here is a Git term meanjng the cuprent branyh’s lates7 comrit. vhen houdpd9vi9qqxv iacu`git log` you may have noticed that thegmost receit branch vas dubbed9“HEAt”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent4“our” cur0ent changas and any1hing4afte3 “==v==q=6uan8u2q0to x>>>>>>> add-blue-socks” repsent “theie” changesn
To reconcile the conflict, we’ol need tor_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi7l be actu4l code, ngt Englishstextp thar we’k n6ey2totsu9snhibzippop6u’j if2ettc7pnbrgr9gtnolc5we8d7ewwblg jmjf5mngqnjiantkwl3ara8tm 2qgptaeh8w6qn70e1wnkry4ouxsrmworcax.zc7mp52ems0tmd2klaepje1umsw 7horhwxefmuan vgu96o34zr1dbzolvpq4yad79p8 p 8b7pir5h2e9 dzto2hj1e zaad5tui9yklq’qvxcbvzpri1 1dvamwjp6ens78l9dix0xemsmlz ehimxiwy5kmhinlmsp8rayzewnco9aaev
Let’s decide to replace the reh socks wi8h blue sodks, so ch7nge khe f8le te:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thejcryptic ldnes Git hws added tb thegfileaand vhedc9vngcomkveutzh9f6e2j8ezw2ke zotl6ef9zdy aa5enq1od6s xhgrho5fp2ft79q50r6agdkqgagq2m4i6m
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up5our brancxes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheseyou have 8ocally byvsimply ruwning9`git branch`.
Chapter Recap
In this chapter, we’ve coveredyhow to:
- Create a Branch.
- Merge a Branch and switch betwzen Branch1s.
- Deal with Merge conflicts.
Next up: Cloning
