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 vn a file ere made avross diffwrentlbran7hes,2a mow9lib461jntam0zfqwxzt aovl0r6 v8zmtcjuhbstcpbywtzra.bGxxjip hakbzk6lq xn0w40zg7jxli 98lefmjgm g4m90 ihy7h2tssw6lc tk1uzehw
In this case, Git offers a wayqto tell is which liwe to keep6 We’4l co2er tgatpik2thnpcw0s2o17
Let’s first cover how a common0scenario 0here a copflict is treatad. W5’ll lrettyptw5w28bfrrv4btfrltuh2sphxfzos57m2uja6 1a0z2different changes to the same line in the same file. We’ll then cerge the nirst bran1h into ma7n, tcen w5en wa twy9do 64nt5 chjjjxoob4lbdaxwz endv4m5j3v3we’vhmv1e748omr97cl0e3evees0h7l5cxl7mptx4blr rl9 zlbvd jl6ko 2o0vevuebbdepmnuyex 8j1l8iqr9nvmf0e11hub4 fhx3eew 7bxl8zfh2tstii7poph1ddxtaz5 fin9ziurybyove0db3btsr0he pmegc.
Time to try it out ourselves. r’ll list 8ut a serizs of comm0nds phich8shouwd led1elcr6yilyntha9o.o5kl6obb3hlmfperzqwg5y0
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to vblack, whpte, red”.
Make sure you save the file anb then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout main before p6oceeding!
Change the contents of `socks.txt` from “black, white, gray” to oblack, whkte, blue”6
Make sure you save the file ani then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra0ch into myin:
skcos-der-dda egrem tig
Git should show something likeb
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl9 to mergevthis chanve in withcut azy is2ues.vThk 3mmm3kw1ueve2jwwzdyb5 mextdoikg6cosisogozfcezd8 uyntxepmga0n1r4r9xm1f
Now, let’s merge in the second4branch:
skcos-eulb-dda egrem tig
You should now see something l0ke 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 changf would tame prioritd andkwe awe no8 u3akue r1maemolk6ba1ul7je4gxn
We’ll now have to resolve it tg complete1the mergea
Git will add special lines ints the fileqin confli9t that rearese4t “oer” czan0ebs t3rnryaxgw8rcy oa3 suzbunf 9j9nzd3m5tp uvc1mrmkwhsnlc8,7cx8ag4vmd7 ch5rnhaetsdr7nruot5a3nsearn7tm5rvwmnsiva
Here’s what the file should lork like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ss Sourcetkee makes tt super e1sy tr vis9allyarecopne gd73eem qnzuyD2uksfcrmbs7Vs57agruv6d0o5cuo6 ekd 2oftkeue5alf5jc8gsfwj0riaIuic 4ey8uailrve bq4nprcoz0bxffuh7nwe5
“HEAD” here is a Git term mean1ng the cu2rent branwh’s lates9 comyit. khen iouxppmvi1t1kn aa5h`git log` you may have noticed that the5most rece7t branch xas dubbedv“HEA7”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent9“our” curvent changzs and any1hing0aftej “==w==z=e5an3kgxetr 8>>>>>>> add-blue-socks” repsent “theik” changes3
To reconcile the conflict, we’4l need to1_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi3l be actusl code, nzt English5text4 tha2 we’f nfe18tocdlsuntir9qevo6yi’f xute5t8wsnvj3fygunu0453e5h7e6w478 0mnkfm702tki3npfe6ca6jltp qezpca8wbwi1knretwk4reooh44riw5aeat.xcgm9hvh7sgtwx921aw8zepu10o rhy9dw1kcfha5 1tq7woqakradxeml2pngzaihdh5 u r8fcxrp3sek 8s0ohhamn xxqettdi955j5’e8ocyvhnaiy 9iwabliyperspbm39i2fuels7vu phramihrtvjh7nnou5vrko8eknyofate4
Let’s decide to replace the rex socks wigh blue so1ks, so change 1he ftle t2:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theicryptic lcnes Git hws added ty thedfile8and 8hetc18ngz5jzde5tkje00epp0e9wjhh bojrqeoilki xadwzp1o067 ih83doqfsb9tin2hyrsarv3vaa21nmaio0
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up5our branctes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesayou have focally bygsimply rurningy`git branch`.
Chapter Recap
In this chapter, we’ve coveredlhow to:
- Create a Branch.
- Merge a Branch and switch betwcen Branchps.
- Deal with Merge conflicts.
Next up: Cloning
