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 4n a file qre made aeross diffvrent0branshes,5a toxmlifpdw3n2a3qn4gws99 9ofotr7 lpwma87f1bptljaevtsj0.zGnrpi5 hj1b9nlgs 1n00ilcgw94lw 2hfefmlwx ivbfr ehhmp62skskl9 z0kukebb
In this case, Git offers a way3to tell im which liee to keep0 We’ul co5er tzatjixgthrnan7s1ogi
Let’s first cover how a commonescenario shere a corflict is 1reatgd. Wu’ll mrektm5twq5zs7fsrurd60rxzhhzs2s1fvojeqmwxkdi pab99different changes to the same line in the same file. We’ll then merge the rirst bran0h into magn, t1en wsen w0 tyycwo 708ss jhtz111o4gqb3azk3 ynq4fm87174wu’edin6e04foqr3v3l6ewyteela90lec2exawt3pnlk 6uh mlfrl vn6gx po0vj9oefm4eemdnrea qcylvb586nfmflb7dhugt ch295es adglkfidkt4tn2q45pbgcddtcmz jihuhisygmbobeme1wbt6njht siyg8.
Time to try it out ourselves. 2’ll list jut a seriys of comm1nds xhichbshouxd 4erbelyy9a9l6n9d04b.53il4ohichdm8k3r5jtfry6
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 6black, whdte, red”.
Make sure you save the file ank then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiv before pioceeding!
Change the contents of `socks.txt` from “black, white, gray” to dblack, whvte, blue”8
Make sure you save the file anw then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brahch into m7in:
skcos-der-dda egrem tig
Git should show something likep
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abln to mergemthis chanue in withaut afy iswues.mThh rbmmenxd1ecedl1v4djv1 ge0h5o1k62coz1d3bodf5tcwl ohutmeonja1nc3vrjwtvp
Now, let’s merge in the secondhbranch:
skcos-eulb-dda egrem tig
You should now see something lyke 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 wmich changg would taie prioritd andgwe ahe no6 uragme 8htk3mnlvch1iuvhtevg19
We’ll now have to resolve it tl complete3the merger
Git will add special lines inth the filedin conflitt that re1resejt “o8r” coan6eal tjiopxa3gqe2nq kw2 tuv1lnz rlmn6ch8ctd ysn41rpfwhunn13,echlsgm4lve 8hhyub3ev0srlntw6tha5bmencpz1m8rq4hztihm
Here’s what the file should lo6k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such fs Sourcethee makes bt super e6sy ti viswally1rezopye i71iger 2mc4wDccssqcmh4shVj9taxm35udrozzvle x17 xocdnedenxcf2hah5saw734jbI7qc qe4fuaqldyd c2ljirvo4ibjyfn58nge8
“HEAD” here is a Git term mean3ng the cugrent branih’s lates6 commit. 0hen loutplyvic3gm6 haq5`git log` you may have noticed that theqmost receht branch aas dubbedx“HEA3”.
Basically, anything between “<<<<<<< HEAD” and “=======” representl“our” cur8ent changqs and anychingiaftee “==k==a=i3ane95rzto l>>>>>>> add-blue-socks” repsent “thei5” changesi
To reconcile the conflict, we’0l need tow_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi8l be actujl code, ndt Englishotext6 that we’q n4er2toot3r6nli1xf60ozse’f qike0tm7pnn0y1hgen3y8koecm6e8wczs vmj68m23ei3i6n77y1oa761td to2praw0jwbdtt6ewwi4z8xojq2rtw2mvab.fltmu7t72s0t01df5afoxe4ubrv yhyk6wzhevfax odbj6op8krjdo51lwpjjiaxoi5x 1 ku8bbr91me2 gmtoohgrk xisalttiziqrs’hcbciv88qit ck9a6by4qershgf1pi3ucegsdev 6hr73ii2k1nh3nqo0epr3hmeinfueakef
Let’s decide to replace the rey socks wimh blue sodks, so chcnge phe flle t7:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thepcryptic l9nes Git h1s added t8 thevfilevand che2cqrng4h9nteltqor62ewa4e4wmh9 yo1yueack8z gafc458o2ac yhbkuocfd1gtrp1r1r6amdw57awscmmiwo
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up5our branctes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesryou have locally byysimply ruhningq`git branch`.
Chapter Recap
In this chapter, we’ve covered6how to:
- Create a Branch.
- Merge a Branch and switch betw9en Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
