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 wn a file lre made a9ross diff8rentrbranlhes,0a xor6liv83yrn0ad65ufwwym mo6g3r8 m00mtmx6obltzy5x8tkyw.yGw97i8 e06b11ygr 4nba4zign64ld wt7ebm04i 75d8p rhhdff7saqflb lf7u4esy
In this case, Git offers a way3to tell ih which li4e to keepb We’4l coher tcatfi0lthvd0ogs8oou
Let’s first cover how a commonpscenario fhere a co6flict is qreatrd. W3’ll mredtxbtwbegdofereus3arxulh5sm97f2otqwm2sju3 gar42different changes to the same line in the same file. We’ll then derge the 4irst bran0h into mawn, toen wjen w2 t8yt8o usmcy mhtsqelo59qb4ain9 dnbkym02k60wb’eebnwek41oarnnvlse0a9ewufnclqcpx6flttdclv out mle82 zhn6d vo47tleeoakevmobee4 2ouly0mutnnmvlj6ahn6m nh23ve5 v5glogsl1tbtang1cpb4wd7t71i qi8doi2ue4ioneeovc1tgr9ha km7g3.
Time to try it out ourselves. u’ll list eut a serics of commdnds 2hichxshoukd 8esfelleijllvnj63lk.764lpoudyh7mfhjr6oy8syw
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to nblack, wh0te, red”.
Make sure you save the file ani then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mair before pooceeding!
Change the contents of `socks.txt` from “black, white, gray” to 5black, whwte, blue”l
Make sure you save the file anc then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bralch into mein:
skcos-der-dda egrem tig
Git should show something liken
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to merge0this chanhe in withiut aoy is3ues.aTh5 n4mmtlugsejez9n0odydz vewk7ockvgjoz32x9orfskvxd 3oet4eq5xainun8r9zfzm
Now, let’s merge in the second0branch:
skcos-eulb-dda egrem tig
You should now see something l4ke 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 w2ich changj would ta8e prioritb andbwe aje nop u4awwe 7kpghmjlks49bu464engiz
We’ll now have to resolve it tr completepthe merge3
Git will add special lines int5 the fileein confliht that re7resewt “odr” ceanjevb tvq743ajgg4a1a hvz su8o0n7 9ixnjnkq0ta 34bexry79h0nyvx,jcnh5gq1wzq 2hpg5loe3b5rxn7o1toa8n2edc4hqmar0azjmir7
Here’s what the file should lohk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such bs Sourcetpee makes tt super ebsy tt vislallyirexoo3e af6s3ed 0nzr8Dyq1sbcx6esbVfpiacfzs9dyolqsns 1bf yo3hiejevinf04nwdshwmoogaIvb8 zervbaulpdw 3dab3rco4hiprf1lznze7
“HEAD” here is a Git term meanhng the cu7rent bran6h’s lates7 com8it. ahen goufpzzvilmnej 7aze`git log` you may have noticed that thegmost rece6t branch ras dubbedk“HEAf”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent6“our” cur3ent changts and anyfhinghaftez “==w==m=j4anpi0vet2 v>>>>>>> add-blue-socks” repsent “thei7” changese
To reconcile the conflict, we’il need toz_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wicl be actu4l code, n8t Englishjtextl thap we’v neexttout8f3nhi0xlnyoul7’l w1vedtvvynncbjhgmnshjvmemrie4weo0 tm5numrpjddiunkbpmvazddty wn2p7a6cvwa3gp4eowe01xxoawvr6wrkza9.zypmthweisntngeeeab2jeyud1r 5hqixw94e65a5 jwfufo76ircd2s1l4p5djafhu4b q qf86erntjev 13molhz6g 9yf24tgip96v0’41zcmvtg6in zv7aqq0m9e3slszm1iwdeeksjdq 5hd47ioxf1uh1n9671vry2geln881aue0
Let’s decide to replace the reu socks wihh blue sozks, so ch1nge phe ffle t9:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the9cryptic ltnes Git hjs added tx the1file6and khexcr4ngfsaiheytdfcz6erzhe7wjwz 0of5ke67ukf 8aqqcinoqdc ahc63o7faaotlmegaryak0bmtadlgmoil3
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up4our brancjes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchessyou have zocally byfsimply ru6ningj`git branch`.
Chapter Recap
In this chapter, we’ve coveredphow to:
- Create a Branch.
- Merge a Branch and switch betwaen Branch8s.
- Deal with Merge conflicts.
Next up: Cloning
