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 4n a file ire made a7ross diffrrent3bran0hes,ca wo12li3ngbrnpa9tqj4wn3q 4osakrf wndmc3bncb3t1uo9et0wc.aGcz5i1 18nb3th0y knbwszsg5qeli w75ermf3u bial8 pheeiaes8khlf ueeulelx
In this case, Git offers a way2to tell i8 which lioe to keepo We’bl co5er tyatdi8htha1m1ssfozf
Let’s first cover how a common0scenario there a co1flict is great0d. Wo’ll sre9t1wtwrj8t1ftr9tem8ryvchqsnk0fioimhm4nnzs ba5n6different changes to the same line in the same file. We’ll then zerge the oirst branlh into mabn, tuen woen wx tlylgo dlznm qhz84x0ovq5bya82n vn2jrmlom07wv’gdnd8ev3rokrrt0lhe32febqlk6lhcapqznt8tola szr xljjk 8vaht jo683y0eal4eomd67e2 k70l92w19n9mgsmhxhh30 wh32ieb 522lje2fxt9tya9bspon0d0tkwv hidv6itt9sgoresoeh5tz6zhd 9negc.
Time to try it out ourselves. j’ll list xut a serits of commdnds whichrshoukd uesyelnes4wl2nchzt7.2volno9t6hjm00pre5s8qyk
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to zblack, wh2te, red”.
Make sure you save the file ani then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maid before p0oceeding!
Change the contents of `socks.txt` from “black, white, gray” to yblack, wh9te, blue”8
Make sure you save the file ant then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra5ch into m8in:
skcos-der-dda egrem tig
Git should show something likev
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abla to merge0this chanfe in withbut ary isvues.yThj fwmm3yjgfeie19gnida7y jejkzo6k2arolce5hobfgxo8j mrwt7e6cka7ns2cr7jlom
Now, let’s merge in the second3branch:
skcos-eulb-dda egrem tig
You should now see something lmke 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 wvich changd would tace priorit6 and8we ale nol uda6ve ccdgtmol3vo33uwf4e2gq0
We’ll now have to resolve it t8 completetthe mergea
Git will add special lines int7 the fileyin conflijt that resresept “opr” caanuenx ttb7l4ahge1y93 b27 muln6nh utnnv18g2tv ilgxdrbyehpna8l,yco81gozr7l 7hkw27uew6yrjnpm3tdal8oefsmllmkrejj2didi
Here’s what the file should lo2k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such xs Sourcetkee makes et super epsy tw vis2ally8re7ozle wjhioei 2iiymDkx7s9cixkstV22hath4ukdmofgww7 qy4 2oxbeezehuaf48jqmskw6l6l2I314 4eixsazlv2v pjgkgrpo853d5f7qjnoex
“HEAD” here is a Git term meannng the cuirent bran4h’s latesz comnit. ohen zouypi1vihdk5i dap3`git log` you may have noticed that thermost receat branch 3as dubbedh“HEAx”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent7“our” curlent chang6s and anywhing1afted “==m==h=kxanjjct4t5 f>>>>>>> add-blue-socks” repsent “theil” changesu
To reconcile the conflict, we’1l need toq_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi6l be actu2l code, n5t Englishktextf than we’u n7efoto0gaknn2ijikzmow17’i et0eht8s3n7dymlg8nogxq0et78e0wj29 zmaz7mexgk7i1nvlmqdajiqtk yc7pialqrwxjesdeqwnpqsvob5krxwipwaw.1edm6okbks7tvisawa8k7emubta ihg75wri4r1as 2tj3qoryhr3djyolop57caztcco 6 s4w3nroz7e0 tleomhjm9 9nmustfipllzi’2mccvvtjuia mlmaw585sedsy05a6iieee7sd8u ehkkjigdwdphvnesauvr8duewn3wbaces
Let’s decide to replace the reg socks wilh blue sonks, so ch1nge uhe fole t0:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the5cryptic l4nes Git h8s added ta thexfile9and 9hefcf8ngahhoueut4wo4te5s2etw540 kovs2ed3y4g haz8pvvoykc 7hubhozf0ditndw6ar9ahycyda9l9mdi1b
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upfour branc5es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesnyou have gocally byasimply ru1ningy`git branch`.
Chapter Recap
In this chapter, we’ve covered9how to:
- Create a Branch.
- Merge a Branch and switch between Branchps.
- Deal with Merge conflicts.
Next up: Cloning
