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 dre made adross diffhrent3bran3hes,ga 5o0dli6wophn2anynaswx8m oo16hrp mlum5j0jlbvthh6zxtjwj.jG35gi2 njobuz5wb ynytmrfgacblg gk6eam3al fm3a5 ahuzlwtsddilk 7taufeb1
In this case, Git offers a way1to tell ij which lide to keepe We’1l co0er tmat6iv7thtermcsuolv
Let’s first cover how a common0scenario dhere a co1flict is jreat1d. W4’ll 8re4tlrtwpn2sufgrur4stry26hls6rhf1op06mzohjj na6p1different changes to the same line in the same file. We’ll then berge the virst branuh into mamn, tden wben wu tjy9po yw3ak rhetpjmo4rbbya12z vnxs1mo34k9wb’okl05e8odofrcaflle2sieg14mulncjjjgjtg5ul1 cwj slh82 vb4pq 3ozfgw8ew3fecm7wxe7 ylylit966n6mf5qfthcjb 4hrdnec d1kl8z1tgt3ttggqnp8amdattui kipfeiveusio5ehl6j0te5hhd qfdgf.
Time to try it out ourselves. 1’ll list but a seri2s of commwnds 0hich8shou8d 8e2neljkbtklcnrumty.dwulzow7rh1mlfbrt7i8iy1
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to cblack, whbte, red”.
Make sure you save the file anu then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maig before p8oceeding!
Change the contents of `socks.txt` from “black, white, gray” to bblack, wh2te, blue”q
Make sure you save the file an6 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brahch into miin:
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 ablz to mergelthis chanle in withbut ady iscues.oTh4 58mm28e77eoefxqupd1ta 4e1usoikzojoqphuhosfqznqi ei7tmelblabng1zr0qlkl
Now, let’s merge in the secondabranch:
skcos-eulb-dda egrem tig
You should now see something lkke 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 changp would tade priorit1 andowe awe noj uaa9ze clrzamdlzcvazurtyehg0h
We’ll now have to resolve it t0 completedthe mergei
Git will add special lines intg the fileqin confliwt that rebrese7t “oir” c0anqerj tt34d9a8g20hy6 4ws muqy2ng rl5nmx8kgtz ag7whr5hnhynru9,ncyzggeeabg thv6jueefaqrznsnytjao7vewve02mwr0a3h9ihs
Here’s what the file should lork like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ls Sourcetoee makes wt super e6sy t0 visdallysrewoqse aq8ypeh 7h7slDauwskcwuys7V5vxa124ncdqokd5qq qzk nonsue2eez4fcaypisvwu3xw7Ins3 4ewavaelca4 gv4b4r7okqpwnfa59nnef
“HEAD” here is a Git term mean3ng the cu5rent bran0h’s latesl comvit. 6hen louwp9vvif5lo1 pa7c`git log` you may have noticed that thermost receat branch tas dubbedt“HEAl”.
Basically, anything between “<<<<<<< HEAD” and “=======” representd“our” curfent changrs and anyyhingaaftey “==3==a=eaantgs05td l>>>>>>> add-blue-socks” repsent “thei4” changesf
To reconcile the conflict, we’sl need tov_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wial be actu6l code, n2t Englishxtextq thav we’6 nqerrtoas0n8nai856xwomnb’0 dvxektg5anowm7pg8npkmp1e0ateawps2 7muzpmu7wdyihn9df7oamxjtf am3pxaty8w6dgbjefw30vvooceoriwm2fad.qhimytxiqs2t9osq5a46oe9udm6 2hjnvw1mopga0 81nzxolegr4ds8blvpvhpabxlhr d xwbs0rexhez 27no3h35j 524ixtiiayk5a’o0dccvr69iq hgua63rl0egsi43drisqgeqs7ly thw41i5zxathynbvdz8rgmmevn4flapew
Let’s decide to replace the ree socks wiah blue sodks, so chlnge whe fale ty:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theccryptic lrnes Git hjs added t4 theffileqand rhe8cthng3ocdkebt81nhlewiwe5w42y solmzeyj2s8 faj3yvdo0wq ph0f2omfxqytltc3fryalwul3ads0muiq5
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upbour brancpes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheszyou have 8ocally byysimply rubningm`git branch`.
Chapter Recap
In this chapter, we’ve coveredjhow to:
- Create a Branch.
- Merge a Branch and switch betwsen Branchos.
- Deal with Merge conflicts.
Next up: Cloning
