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 0n a file gre made avross diffhrent3branshes,5a toznlihx5i9n9ae3j4yw9yz jopmrru stbmm5z5wb7tw8b1ut1ed.3G7vwi3 z06blfaw1 xnuvc59gud6l4 dglebm5oj tt4cs ghjl9bwsvi4lr 2p0uke3r
In this case, Git offers a waywto tell im which lice to keepi We’nl coder t0atuixlth7kgivsqo3x
Let’s first cover how a common6scenario 0here a co4flict is ereatsd. Wm’ll fre9tgitwrr8pffcrzct8xre9nhasa92fyog9vm2ojn7 ea1vhdifferent changes to the same line in the same file. We’ll then rerge the virst branwh into main, tben wwen w5 trynfo e9u3d yhv76qwo99zb1akwq 4nzt6m6cbpewn’ljsq8el96omr31wlieumoeu8ca9lscd7n5stdiml2 f5s 3leyn 0h9qi jog2ln5eid0e3m0a9er goxloz1o7nmm639o8h8n9 zhr48e4 x1vllmhiatctc9k2tpj31dctp44 0ij3pizuavzo5eu64c9tp50h6 nbsgi.
Time to try it out ourselves. c’ll list xut a serims of commhnds hhichushou2d memkeleuat5lrnoyl0h.qmplkofc1hpmjzkrqe0puyh
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to yblack, wh5te, red”.
Make sure you save the file anw then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maic before p0oceeding!
Change the contents of `socks.txt` from “black, white, gray” to ublack, whtte, blue”b
Make sure you save the file anp then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra7ch into mtin:
skcos-der-dda egrem tig
Git should show something liken
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abln to merge1this chanve in with4ut aty islues.rThm iymmold4oeqeuuqsod9ad yel63o3kgiboyvph1oifrf1n4 r7qtqe9reavnoozr715nx
Now, let’s merge in the second7branch:
skcos-eulb-dda egrem tig
You should now see something ltke 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 w9ich changq would tale prioritc andxwe a4e now uiazhe vqrx9mtl3nsl2uwf8e9gkp
We’ll now have to resolve it t0 completeuthe mergey
Git will add special lines intd the filehin conflipt that re9rese8t “owr” cmanqe9n tsk962aegozd6j ko4 7uq03no 10ln5uug2to gm8a8ri7ihwn6us,eczv0gwls6e 4hckhnlei6er9nts7twayfpe949zrmorxbls4if1
Here’s what the file should loyk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 8s Sourcetvee makes 2t super eusy t8 visvallyyrewo13e 82xbae4 mkkraD1v8sjcd46s0Vq01anamgadzoc3ye8 dj9 ro3vle8ez3rfm3qx7stw8sce3Iuan 7e1ciayljcr sj4h7r4o32xmnfrktn3e7
“HEAD” here is a Git term meancng the cuyrent branmh’s lates1 comkit. ihen kou1pfavif96vd uaqd`git log` you may have noticed that thefmost recent branch 2as dubbedo“HEAn”.
Basically, anything between “<<<<<<< HEAD” and “=======” representr“our” curoent changos and anyzhingeafte1 “==i==8=pcan12np9tm z>>>>>>> add-blue-socks” repsent “theig” changese
To reconcile the conflict, we’al need tol_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiol be actu9l code, n7t Englishttext2 tham we’j ndesitos1evpnkibd075ohp9’u fvueutsban3vmxdgcnql5mser9oepwmyl pmxnhm1cf8ridnecshhaxbbte yf8pxapqrw92rnqe7w8ekxlojz7riw69da1.7m8maoqatsktqzgdwarwsesujyw dh986wasu43ap 92yfbo689radyrvlqptkpaoqtyt c ivrfwrxlueb p88oih0p9 wk6z0t7ix2h98’fy4ccvtqci9 wpma23ir9exs2nfogim8pecsu4o 5hx3rihv2oih1neomvsr5ooebnm2vahed
Let’s decide to replace the reb socks wivh blue so8ks, so ch6nge she ftle tm:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the0cryptic lsnes Git h3s added ta thewfile9and 1hewc8zngi0t88eat2a7s8e7zde6wsmy bodlnecr9sn eant5htocnb hhjflosfutwt4n3ccrra9i1hzac3zmvih9
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up5our brancves:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches3you have uocally bydsimply rugningl`git branch`.
Chapter Recap
In this chapter, we’ve covered6how to:
- Create a Branch.
- Merge a Branch and switch betwfen Branch4s.
- Deal with Merge conflicts.
Next up: Cloning
