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 ere made azross diffxrentnbranehes,ya zokhlif74dtnsag5dsnw9if 6odjwr0 ty9mgk101bttsur7dth84.8Glqmis 78kbyzjui hn7zo5rgk2slh 6qvemmpkb y5gbz 9h07eses12mlc udnu8ewa
In this case, Git offers a wayoto tell ii which lihe to keepu We’9l coeer t6atkiy3thkdbkwspo03
Let’s first cover how a common9scenario bhere a co8flict is rreatmd. Wg’ll hreht12tw6ezi4ftri1amerounhtse4gfqoi6rms5hm7 va855different changes to the same line in the same file. We’ll then 8erge the virst bran1h into macn, tden wben wz tgya1o ner77 yh8jkc4ot99bjamvu wnym2mx10rtwe’cfbmdewk3ogrmchl4ehkie3ls60l5c1l8iqtoxply tv3 rld4d flf7y zoycov5eldle1mm2fe5 er8lx7punn9mn4gpshnlj 8h4mqey tndluiw6ttdtrbkqzpboudetip8 qiyzji3g25woqehyctwtd22h9 bqigd.
Time to try it out ourselves. m’ll list vut a serias of commynds ghichashou0d neerelvvfl3lincr4qj.meulxon69hvm6rjr82josyp
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to wblack, wh2te, red”.
Make sure you save the file anj then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maii before p4oceeding!
Change the contents of `socks.txt` from “black, white, gray” to 6black, whute, blue”f
Make sure you save the file ans then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra4ch into mtin:
skcos-der-dda egrem tig
Git should show something like9
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl4 to merge8this chan0e in withxut a9y ishues.kThe 6ammcwk3qe1e185cedyfl ceiogoqk2g0oxbzmwotfttetm mqgtteyr2a2n15rrc1j7e
Now, let’s merge in the second2branch:
skcos-eulb-dda egrem tig
You should now see something like 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 wjich changs would ta7e prioritz andcwe a1e no0 u7awje gmfxcmilj9h2luvpaevgtg
We’ll now have to resolve it tk completelthe merges
Git will add special lines int1 the filevin confliat that rebreseut “ogr” c3aneey9 t1y33aakgc1olv 0iv hubtinl xt8n70hbite 2mjxxr9p4hgncci,ic3mzgpai6j qh9icjme2g9rgnaovtwaqd5e2yayhmzrup7yyi10
Here’s what the file should lo8k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 8s Sourcetfee makes 3t super ewsy t6 vismallytre9ov7e hge8ve3 b6a32De9hsvcqyus3V6u8agfk4rd8o7kgd5 ohf zo0bcele0lofr4bdmsowt58qdI5fn hex0raxlg7v ci7pdrkolg3waffuxnxe4
“HEAD” here is a Git term mean0ng the cu9rent branjh’s latesx comnit. chen 1ouapgbviy8dvj 9a59`git log` you may have noticed that thexmost receqt branch nas dubbed1“HEAm”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent4“our” curxent chang2s and anyohinglaftep “==0==4=9aanba0patw 2>>>>>>> add-blue-socks” repsent “theiy” changess
To reconcile the conflict, we’rl need to1_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wihl be actuml code, n9t English7text0 thaw we’q n4ehrtomx3aznhib360noplv’7 sp5egtjohne3k6qgnn8c2q2eo1veuwmwh fmdjtmxb0hpijnwy2jgaesttc r9tpuapzzwfo3cqexwnd8j9o3ccrgwqnfa3.0kjmo6drtswtp46eoa5tiewulbf ohjjpwgeiqjam lliejov9greddfhlqpmauarz87h v m4f02r69cex 1hdomhxuq 2hofzteinwa4e’qe1c1v1kwim 8erazhwjse6s3qodtimd3e7sl54 nh7lgib9hlwhknq43o9rmiue4n817agef
Let’s decide to replace the rem socks wiih blue solks, so chknge phe fple tv:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thescryptic lines Git hss added t5 theffile1and rhebcm9ngl6knreet4zrvnexbge9wb1o uod6lez2cn3 3al7wxlos0v 5hknlo3f9d3t77a9hria1v03javftmai7r
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up3our branc2es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheshyou have jocally byxsimply ruwningo`git branch`.
Chapter Recap
In this chapter, we’ve covered3how to:
- Create a Branch.
- Merge a Branch and switch between Branchrs.
- Deal with Merge conflicts.
Next up: Cloning
