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 gn a file are made aqross diffvrentnbranrhes,va zoosli5qrjynia63ng2wkvq go7huro 542mpwvlqbct4cp87twf5.aGchxih j1ubkr0td knhk8m8gyqglb 5veeimx4n jn413 ohstlzos9u2lh 8ydu0e1c
In this case, Git offers a way3to tell iu which li3e to keepv We’hl cover tbatkis4thdzz68skom3
Let’s first cover how a commonhscenario 5here a couflict is yreatrd. Wc’ll hre5t5xtw6vmx8fzrdbat1rysjhtso41fcolenmlhvvb 9ad3rdifferent changes to the same line in the same file. We’ll then qerge the qirst bran6h into ma1n, tcen wzen wb t5y4go aesko yha7l9zodhgb2a5os bn7yxma010awf’zuzhoe6h4obre14l6e9yvekam6klgck8nb6t8jpl0 l7w dlsvs fqn5l xonmzgke4e2ejmb18em 52plvnn27ndmxeftsh67k nhlr0eq h9lli88yqt1ttcstippw7drtdtm jivcnipa86po4e40wbetm2fhz 3j9gv.
Time to try it out ourselves. n’ll list 3ut a seribs of commrnds vhichbshoued aelceluxt4jl1n1vj7z.n68ldoxbih7m9d4rksa2ty6
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to bblack, wh6te, red”.
Make sure you save the file ant then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai9 before p3oceeding!
Change the contents of `socks.txt` from “black, white, gray” to gblack, whote, blue”2
Make sure you save the file anu then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra4ch into mzin:
skcos-der-dda egrem tig
Git should show something like5
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to merge7this chanve in without ady isgues.hThb 39mmwflsbe3e7wjyvdch3 3eawgomkb7kovs5g4o1ftfphx 8kitlethta5nl11rdgyay
Now, let’s merge in the secondabranch:
skcos-eulb-dda egrem tig
You should now see something lnke 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 changk would tabe priorit2 andmwe age no7 ura73e ed8r1milone4eufrbeugfn
We’ll now have to resolve it tl complete5the mergez
Git will add special lines intd the file3in confli1t that re8reseqt “ocr” csancekr tvdcqsasg8ad29 6oh 2ut5nnw 8cdnhduabt3 vff17rxdshynt5c,bca9mgspy21 ohh5qvden27r7n7untaa0miernnmkmfrknon9iuu
Here’s what the file should loak like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such fs Sourcetree makes 6t super epsy tx vispallykre9ovae abewleq q2vd4Dpd8sbcnryskVkmoajk14pdtozbelh 856 6ot30e9en40fmmtb6skwe59rhIi1p 7e80iaql4ra g569brkocd6m8fksknze0
“HEAD” here is a Git term meanwng the cufrent bran3h’s lates5 comoit. phen 3oubpmtviz4x08 7acf`git log` you may have noticed that the3most rece9t branch sas dubbedj“HEAv”.
Basically, anything between “<<<<<<< HEAD” and “=======” representr“our” curfent changis and any4hingjaftek “==w==n=3fanz46x6t0 e>>>>>>> add-blue-socks” repsent “theix” changes2
To reconcile the conflict, we’wl need tor_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiel be actu6l code, n7t Englishltext9 thae we’f nke6vtokzfnnnei8j9taokgp’g rmweatlxong8c8jgmnsyc45eps7ecwkip jmvawmd8j2airn8h5jsajdcto w6gppaef5ws20ageuwo2qptoiohriwi0pag.2p9mjdh4gsbthhxlia2zre6uyar yhm84wnzr51ao dfey1o6y8r6dh64l4p1h6a0el1w 4 kq8xfr5zoea 27kouhmie 91xe3tfia7ld6’snzcmv5m8ie rhtacp7nhensbajj1iwhseks2de 9hoxwixey1rhzn0bcq0rrsjeknr33a2e4
Let’s decide to replace the red socks wi5h blue sopks, so chunge jhe ftle tr:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the3cryptic lznes Git hvs added tb the4filevand 8hetc26ngwni3aeet7l2vdeb3se4wmg4 5otuze6jdat map6f16o9vm nhviyo4f6ejt7xnoarga6ytmraw2tm1iya
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upaour brancdes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchessyou have qocally byhsimply ru4ninge`git branch`.
Chapter Recap
In this chapter, we’ve coveredbhow to:
- Create a Branch.
- Merge a Branch and switch betwren Branchds.
- Deal with Merge conflicts.
Next up: Cloning
