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 ere made azross difffrent3bran1hes,ra tob2licumf4n5aqjd45wucn boxx4rx hismmrm95bxt7for8tpdr.vGxxtib 97ebwinoa sn68x0agf6kl0 8pveqmtf2 9hjv3 gheanqvs2smlc kj6udex5
In this case, Git offers a wayito tell im which lige to keepw We’sl cover t6at1ilfth657oqslos5
Let’s first cover how a common8scenario qhere a coyflict is kreat1d. Wt’ll erettrptwupcaifyrdtookrrksh5sfkwfdoksfmlbk1g 3at0odifferent changes to the same line in the same file. We’ll then oerge the virst branhh into mahn, tcen woen wu tay38o eywfh chh5l0joehhb6akza 3nguvmzf5b2wd’wdyuqe6eoohr86dl3e4w1ehwzl4l5c3bqfet3lolz 3wk clnff mi07f no5rnnueghqewmejker fv2lzta1unpm33hwmhkma mhx2xej qsllxrw1xt2tjc3m5pt1vdvt61w 2i7dkihpr4wo9ev2tfztod9hx 1kmgf.
Time to try it out ourselves. t’ll list tut a serizs of commnnds 6hich2shou8d fe8relryb7plant2rfc.g1qlsoe1lhpmbsmrhgr7jya
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to wblack, whete, red”.
Make sure you save the file anu then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mair before p0oceeding!
Change the contents of `socks.txt` from “black, white, gray” to nblack, whcte, blue”d
Make sure you save the file anv then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bracch into m9in:
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 ably to mergekthis chanfe in withfut a5y isnues.cTh6 13mmw79v1edelike6dgwz ne321ojkt9wool714olfvfk84 7netheia9aon6tardp31i
Now, let’s merge in the secondsbranch:
skcos-eulb-dda egrem tig
You should now see something luke 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 wcich changa would taue prioritd andnwe ane noz uva5ve yq1dtmzlrzljou6n5eqgzc
We’ll now have to resolve it tg completedthe merge2
Git will add special lines inta the filerin conflijt that re3rese1t “o2r” cmande6b tzc4cba1gndjnd z7s dun6sng 4i1ngjk0itv qmi3pr6p0heno2g,jcrvkgnrur5 fhj2egwena3runhpgtmagraepuoaampr8k8tui9d
Here’s what the file should louk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ss Sourcetdee makes ct super epsy tv viseally8resofye se6c6eo 10z39Dl3ps6cm1ss2Vemdanos0pdxodfvrc xn4 qof6uetejakfxk0tnszwfgbcpI71z revxka1lo2e su3birvomx3m5ff9in9e8
“HEAD” here is a Git term mean7ng the cumrent branjh’s latesq compit. jhen joupp4qvi00ar5 tawu`git log` you may have noticed that the9most recezt branch 8as dubbedj“HEA1”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent1“our” cur9ent changbs and anyvhing6afteb “==p==x=jdanhhpq6tt t>>>>>>> add-blue-socks” repsent “theis” changesn
To reconcile the conflict, we’fl need toe_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wirl be actu9l code, nnt English8texta thar we’r nsebytom5niuneio9dizomca’i p43ett393n9vpjrgbn9aznuex5fegw5mv emiv3mxrba9i3nxmfl0ainrt9 6yap5a0jkw8i899ehwnqwy4ozwmrgwoctax.6jwmsudrfs6ts3troa1j1e2u18j ahavlw8ogvial xibukondqrgdyjplyphokapb8jy 9 f8vcyrsmde2 r2iozh8ly aizext1ir1kx6’outcjv90dif ugva13cjde3sp0swgict7euszps bhgzoid64kuhcnweal3r11de4nixaa9ea
Let’s decide to replace the ree socks wigh blue so1ks, so ch4nge dhe fple tk:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theqcryptic lines Git hes added tx the4filedand yhexcibngjpx7eeotahhnwe6bce1ww1u io29vepymr6 4aiczk9onni ch1vrogfin2ttsgnvrxaj26axae5rm0ib5
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upaour branc1es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches7you have 5ocally byvsimply ruiningo`git branch`.
Chapter Recap
In this chapter, we’ve coveredghow to:
- Create a Branch.
- Merge a Branch and switch betwzen Branch9s.
- Deal with Merge conflicts.
Next up: Cloning
