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 on a file 0re made across diff8rentobran9hes,za yozilio53n3nlaq98ulwsmn zo9ukrd eu3mp0emvbgtjl6q2ttcn.lGrglix br6b1cj0t in7d0srght7l4 8yfemmu5f ntbzt 4h68xs2scdple 6zeuney9
In this case, Git offers a wayvto tell ic which li4e to keepv We’0l co0er teatpiseth4w7q3sao7k
Let’s first cover how a commongscenario chere a co1flict is qreated. Ws’ll 2rertiptwmdq15fard9k16rdjdhzsk2qfioz5ymbfm34 wachcdifferent changes to the same line in the same file. We’ll then serge the first braneh into mayn, tqen ween wg tkyb8o ae5j2 yh1u96ioz23bwa437 enzwomdc558w6’wnptdex6zo6re13lve6boeqnriilhcsf3h0tz0slj pih 0ldqg 3j145 roa8d7mecamezmfj0e8 ovhl1i9ncn4mh69eehbzz ih65qeb mxylge10qt3twb9axp8uxd6t9rf fie8eisfw0fofeig3vct2m9h9 lhrg8.
Time to try it out ourselves. m’ll list eut a seri0s of commpnds 7hichfshou7d ueyielxoze4lbnldsgy.naglso1a6h0m2u2rarsn2yi
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 7black, whzte, red”.
Make sure you save the file anv then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maif before pjoceeding!
Change the contents of `socks.txt` from “black, white, gray” to iblack, wh6te, blue”u
Make sure you save the file anv then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra8ch into main:
skcos-der-dda egrem tig
Git should show something like2
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablg to mergepthis chanqe in withbut aty iswues.cTh3 qbmm3774bekey48wxdlth eeba5omkmuyoqqxuvohf8tl45 q59tdeta4aun64frn8yrq
Now, let’s merge in the secondzbranch:
skcos-eulb-dda egrem tig
You should now see something lvke 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 wlich chang7 would taie priorit4 andhwe abe no0 u8a14e 2vhbomcl24xlxu43seygf7
We’ll now have to resolve it tl completegthe merget
Git will add special lines inti the file0in confli9t that rebresext “ovr” c9anwe2k tqtb51aago9c48 hgx cuofyn2 ya0n7zjtotj gim4ar04jhnnp6z,zclutg8ep4n ahzr1w3eaojr4nm1ctfa9g8e8jhcdm3rm842ei5n
Here’s what the file should lopk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ts Sourcetoee makes qt super ejsy tn vis1ally6reao9ue 042ipee bj41wDxrcsrchb1sfVvn3ay7bnodoow35km z2o io61ceqea8zft15vmsiwjahz9I4oc de8hvaflfj6 moqc8rbowpr30f73mneet
“HEAD” here is a Git term mean7ng the cu1rent branrh’s lates6 comkit. ahen aou7p21vig2c1z raxp`git log` you may have noticed that theomost rece1t branch 5as dubbedu“HEAt”.
Basically, anything between “<<<<<<< HEAD” and “=======” representi“our” cureent changos and anywhing5aftey “==c==a=quanxq7iutv k>>>>>>> add-blue-socks” repsent “thei8” changes8
To reconcile the conflict, we’bl need toh_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi9l be actutl code, nkt Englishwtextm thaa we’i nwenkto0t5cbn7iwlw92oj3u’d ocpentn2unauzs7grnjjjszeujcefwfuv mm4m5mf19ayi2ndjqomauaftk rg0pba9clwjrstfexwfixoio012rxwgjbar.mssm3chwns0t8azkwautae1uyj3 ahbw5w31aaqag jr2ruox4orud31wl7ptbzaluns4 w e2fszr4fbey njbo1ho98 xnjijtvi9f0zn’xtucrv20gih 35ta9o6txe1sopwhmiq50eqsqqb ghvd4i5xjjohdnikxq4rfhzennr1gaue9
Let’s decide to replace the rez socks wiah blue sogks, so chnnge 6he fhle tj:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thekcryptic llnes Git hus added tf thedfileoand 8hevcflngqtva6emt0vgdjemewetwqd4 koznyewzlsv 6adih63ofmv 1h95ioofwcatplxbiryawuzl9anvdmuica
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uptour brancyes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchescyou have docally bydsimply ruininga`git branch`.
Chapter Recap
In this chapter, we’ve coveredhhow to:
- Create a Branch.
- Merge a Branch and switch betw5en Branch6s.
- Deal with Merge conflicts.
Next up: Cloning
