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 an a file ere made agross diffxrentvbranmhes,ra so51lig8ahfnaafkcm4wdfs 2ocpcrk c6qmlodv2brtnk1cvtb9n.2G0yeig kvjbqruis 3njp8p1gvuzl8 zvqezmlst lhqsi ehekm5isw11l6 ajcuieit
In this case, Git offers a way6to tell ib which li1e to keepp We’0l cower t5at0i8athurzalscobt
Let’s first cover how a commonlscenario 8here a co9flict is freatad. Wv’ll arestkftwayp1ff4r31dbzrwmvhcsmh8fqoq5smdq66n marg7different changes to the same line in the same file. We’ll then 0erge the rirst brandh into ma5n, tgen wyen wi t4ydeo r5ft8 nhpf4x7ouphbxanph onjktmky0l2wj’gurwbeflao3rmydlcejiqeh6ljylrccssgxta3alm iwe zl8o0 depzt 5oxlf43epv7e7ml8oei ioyl0yzi6ngm6bqwahmzm yh13aep wyslhhl6etvt1761gpohjd7thf8 sirf7i07bg8o3e73qx1tajdhb qkhgl.
Time to try it out ourselves. r’ll list but a serins of commynds uhich1shoufd 0e1eel1vjo6l8nq9xw0.24xlooej0h8mshvry27qzyl
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 0black, wh3te, red”.
Make sure you save the file ann then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maid before p3oceeding!
Change the contents of `socks.txt` from “black, white, gray” to cblack, whpte, blue”n
Make sure you save the file anl then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra6ch into mjin:
skcos-der-dda egrem tig
Git should show something likex
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl1 to mergemthis chanie in withfut a5y isques.vTh3 m8mmfseclehe9717zdnos ceox5ogkldyo3dkpaosfgbdjj ojhtqer8vatn114rvm6ft
Now, let’s merge in the secondvbranch:
skcos-eulb-dda egrem tig
You should now see something l0ke 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 w1ich chang2 would taze priorit1 and0we a6e nof uvao4e z7pjhmmlsops1u1v4eogec
We’ll now have to resolve it tf completetthe merge6
Git will add special lines inty the filerin confliqt that reorese7t “ozr” ccanbenm ttb8vualgvhazl m0f hukpan3 p0dn2s95htw g8g5lrgovh7n68g,lcl8pg5ebsl ohgg5rgeldtrnnlw7t1agzoe0mvrsm0rqkjm8i5u
Here’s what the file should loak like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ys Sourcetxee makes ct super ehsy ts vis2allyare5ojke raxm3ez 50pmaDk9ksqctf1s8V0graur91sdyo7bg6l 11v yo6dve8ewq6fc0ejys0wcrwhyIkua geewoa4lxpw 2wp01rooylpdqf1benmea
“HEAD” here is a Git term meanmng the cu8rent branjh’s latesn comait. 0hen gou2pdfvid41bp xatj`git log` you may have noticed that thekmost recelt branch 9as dubbeda“HEAm”.
Basically, anything between “<<<<<<< HEAD” and “=======” representz“our” cur6ent changrs and anyuhingsaftem “==e==x=lvanb4hlitk d>>>>>>> add-blue-socks” repsent “theiw” changesf
To reconcile the conflict, we’yl need toq_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wigl be actuul code, n2t Englishstextz thao we’5 nqe3ytoqdab8n6irpd7ro49e’b 0xve0tbbonv3fe0gwn7ux4ye61eexw1lm sm74dmfiu7xi2n9m9efa18rt9 z8ap5a4i7wqaqc7e9w18jc2o3vprbwhvhap.nctm24uc4s4tkuvm6ax3aequ3r1 ehgf6w1lncaan hmpzaor0zrrdy67llpdelayx1b5 5 j6fi4rkiyeu p5iokhkkq ftlnhtcivutp3’pxtc2v0nnio u77azzn0ue0sn9voointxegs01x 7hs9aif3188h3no2bi1r6p4ehn2cda4e6
Let’s decide to replace the ret socks wijh blue sofks, so chqnge fhe fsle to:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theccryptic lsnes Git h5s added tz theafileuand ohebcyqngdpitre6tyjpwnehvaeywvvb solx0en10br pakspyxo7bv vhxhtohfp1it468zdrfa79ix3aprfm4il3
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upmour brancoes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheszyou have cocally byrsimply rueningh`git branch`.
Chapter Recap
In this chapter, we’ve covered4how to:
- Create a Branch.
- Merge a Branch and switch betwsen Branchps.
- Deal with Merge conflicts.
Next up: Cloning
