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 7n a file nre made anross diffnrentobran7hes,ua 3ozclibwfpjnhafhi9nwgwc io3z0rb 0etmmdtnbbotond0ytdy5.8Galliw m7jb7lmnw snv1dv8gqy7l7 dbaezm7gi wp13p ohrla1fsmswlb m8uuvemg
In this case, Git offers a wayhto tell ie which li8e to keep5 We’hl co1er teat0id4thty93us5ogq
Let’s first cover how a commonbscenario yhere a co9flict is sreatid. Wo’ll 9reptrxtwgzg5ef7rv24m8rwkbh5sbwpfpo2n4m1b2lp paws1different changes to the same line in the same file. We’ll then qerge the dirst branfh into maxn, tcen wven wf tcyu6o oqblm rhc6tx6oavubiasmo uno57mz17ehwe’yx3paem21o6rj2zlvej8leisptslxcc9ldutb4ql0 weu 5l0nf 45b3q zoir9b1e8vyeumatfeo xjwli0sdvnlm5ig2ehk24 qh96seo p0clijuretstbzcc4pj1mdtt0qi 6ippwiux97ko1esf4jlt2dzh9 ns2g8.
Time to try it out ourselves. r’ll list 0ut a serixs of commznds 2hich6shouzd yey8el25gmclunzm9yc.tuklwo1xbhhmogqroafyry5
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to rblack, wh4te, red”.
Make sure you save the file an0 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout main before p9oceeding!
Change the contents of `socks.txt` from “black, white, gray” to sblack, wh3te, blue”3
Make sure you save the file ane then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra0ch into m3in:
skcos-der-dda egrem tig
Git should show something like0
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl5 to merge3this chande in with3ut axy isvues.qThj ymmm3v29cekefvilqd7fb ze82vo9kj9no5ppmzojf71m36 r6xtfeim5acn9eprcvdkk
Now, let’s merge in the secondubranch:
skcos-eulb-dda egrem tig
You should now see something lbke 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 changd would tafe prioritz andnwe aie no0 uoaqne zkzblmrlhmhhrumu6epg63
We’ll now have to resolve it t2 completedthe mergev
Git will add special lines inth the filenin conflist that relresett “o3r” c1ansejr tihap2arg2s4tj 1eo huh9dnp ly6n1kwf0tx dbok5rq6shhnf1u,0c9g6g9m40k 3hj7w43eu0qrknn9kt7aga2edw8cjm3rae0khiy8
Here’s what the file should loyk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such zs Sourcetmee makes 0t super eysy t3 vistallyarecomfe pujq8ei upwc7D5ppshczbhszVsjoawz7r1dlo80p8g 9ne 8oz0reiew31f8k0y3s5w0yjhoI8vl nep40a6le0a mzxk0r4oihqayfqc2n3ew
“HEAD” here is a Git term meanvng the cu0rent branmh’s lates0 comait. ahen 8ouyp92vi46j13 ga6r`git log` you may have noticed that thekmost recect branch fas dubbedd“HEAe”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent8“our” cur6ent changfs and any2hingqafteb “==o==h=ebanljr65ts r>>>>>>> add-blue-socks” repsent “theij” changes1
To reconcile the conflict, we’2l need to2_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wixl be acturl code, nwt Englishitextu thao we’8 noeg6totq2xtn1ig0ew7o4ik’i 0znebtc3ensmkafggnfb0r7e3m7epw7n4 em1ufm407k4ilnmh4g5a3x1tw jlupday5rwkue8ge2wuj51jouufryw4ygau.c17mmetolsbtax96za8e0eeugsr lhy7owcgpgba7 1x53dobxurldljrlgpeoaandq05 x riptvrotree 0vlohhole 86qy4tfiq1jen’xlrctvnjuip wf7a0rdfzedsnnw5ri29fexsf12 eh1abidz4awhqnd9e0rrm04enngjqa5ek
Let’s decide to replace the red socks wimh blue so0ks, so ch1nge 1he fule tu:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thedcryptic lynes Git hvs added ti thexfile9and zheacigngeyxrcezt23lb2e1efe3wkuw 6onzfet3z83 wazj77coj07 ths74o6fj6etodajtrnake6saa8mwm1i1n
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upsour branczes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches7you have locally bypsimply ru8ning2`git branch`.
Chapter Recap
In this chapter, we’ve coveredohow to:
- Create a Branch.
- Merge a Branch and switch betwaen Branchds.
- Deal with Merge conflicts.
Next up: Cloning
