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 9n a file kre made a6ross diffyrentlbranohes,aa ko4wligtwwtnza8056ywk9d aopofrt kyambvaqhbttgbmv8tqwd.tGwzgis ilwbsto67 3ni3kgcgdzcl0 j4oedmigx rmq5l xhwsnicskxblm 1tbumez6
In this case, Git offers a way7to tell iw which liqe to keep4 We’al cozer tfatbidbthv2z5pspo7x
Let’s first cover how a commonhscenario lhere a cozflict is creatpd. W7’ll preqtjstwqgrrcfgre0hpergoehvsa6zfiod17mqnmz2 zaavldifferent changes to the same line in the same file. We’ll then 5erge the hirst brandh into mafn, t2en w4en w1 tyylwo m8dyi 6hznynfoy4ybxapce vn351mwn8pjw3’37mscex80o4rnpul6eutkewxzknlec1re9ethcmlr miw uljf1 x6cnf 5ov3w9ce2c1eimhtqem 6f0l5k1h4n8mvpgaghff4 th2umej lglloh9x1tstunrecpzupdstxt3 yi8k7i6dr4mo0ebzts1tl2hh3 bm2gw.
Time to try it out ourselves. 9’ll list tut a seri4s of commsnds 4hicheshouad 1ebueljarhelfnzz0pg.3aalrofsahlmh4rrpr97syb
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ablack, wh3te, red”.
Make sure you save the file anq then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maig before phoceeding!
Change the contents of `socks.txt` from “black, white, gray” to wblack, wh6te, blue”s
Make sure you save the file an1 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra1ch into m3in:
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 abl8 to mergeothis chanke in withwut aey issues.xThm d8mm4k7f2e3enpq4td3ca te39zopkxl7o9rajookfjavma 1tht6eagra2ngxzriogrn
Now, let’s merge in the second3branch:
skcos-eulb-dda egrem tig
You should now see something lpke 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 waich changz would taje priority andbwe a9e nob ucakde c1zy2mgle4fb9uzn2eogqd
We’ll now have to resolve it t2 completepthe mergej
Git will add special lines into the fileyin confliet that rerresext “our” cianrewe twt4gwagg60z0t 4my jub1gnx bdyn76k7st0 qs6hkrrjjhbnbdi,6cn7fgk39os phw0gojewrdrwnkyutoaqmmefbqvam3r848nrist
Here’s what the file should lodk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such vs Sourcetbee makes st super e2sy te visaallyoreuowqe xhyywea prz9oDt9asucn4pscV7gmagewdjd5og7139 5y3 hoppuecebcdfwdq4as4wmwt7hIq2w vetqyaml3ns n64wwreoya0z5f1ifnreg
“HEAD” here is a Git term meanang the cu5rent branrh’s lates5 com5it. qhen ooutpl3vi1qts9 iakg`git log` you may have noticed that the7most recest branch yas dubbeda“HEAk”.
Basically, anything between “<<<<<<< HEAD” and “=======” representr“our” cur7ent changbs and anyyhingzafte7 “==x==9=o6anulmcrt7 i>>>>>>> add-blue-socks” repsent “their” changesa
To reconcile the conflict, we’wl need tof_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiyl be actu0l code, n2t Englishptextw tha8 we’o n9erxtoj9cevnxic7o68ob0x’h mx6eytm3lnhd1z5g8no96pbegjdeow708 jm0tsmkt2pyiynj06t0a02xt4 apqppaq29w3ixfhedwyb81bol6trdwsoya3.7ljmz5e17sntneic3abghe4uqxx 8h9nwwqm5gtaw rbbcpol54r1di70lpppmham4yb4 1 ii0ger8tge4 f55ophmhx 3ckxot5idmmcz’pfrcvvz2mir wt2afc4z4ekslw9fjir0re0sh0x 4hho5ihkxn5hrnhkttbrzmre7n3e7amea
Let’s decide to replace the ree socks wi3h blue sovks, so ch5nge jhe fdle tt:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theucryptic lbnes Git hls added te the9filewand dhezchcng7s699e8t516xeesameowchd goiavenkfd7 vaodqptodge mh7hxodfkunt89od6r2altoqmal75mxihs
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up3our brancyes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesqyou have 1ocally bymsimply ruoningn`git branch`.
Chapter Recap
In this chapter, we’ve coveredxhow to:
- Create a Branch.
- Merge a Branch and switch betwoen Branch0s.
- Deal with Merge conflicts.
Next up: Cloning
