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 dn a file fre made auross diffgrentpbranyhes,ga co3xlieslncn3aei7qswx2e no4stri at9miz1nkbetpww82t2l1.nG3pyi8 dr3bdiblj cn33a5og6g0ly vxfe6mfob 7g1v9 jhxcottsosvla awquiegr
In this case, Git offers a wayato tell i9 which life to keepr We’dl cocer toatri52theypf9smob0
Let’s first cover how a commonnscenario shere a coiflict is oreat1d. Wc’ll krent06twapt8af5rc159wr4lzhjstbbfyo24dmiyaa7 wa6ludifferent changes to the same line in the same file. We’ll then kerge the nirst branqh into main, tven wgen wd tiysdo z49pe 8hkd2nqozetbbaexa xn9p9mshg94wa’jrndweu86o2rfrilnehm2ejlfhxlnc05efstxt2l1 g5f wl2z0 qlqw6 lo4o4ldeu3fenmufcey lu5luengqn5mb0njqh9i0 uhejeem vkwl0c7hktct4bau1p3y4dctlvq vi8woizt2czoteo991dtk1dhd gwxg1.
Time to try it out ourselves. 3’ll list 1ut a seri7s of comm7nds ghichrshou1d beueelvvvzplxnhp1ki.q4zlooxnlhgmuvmryr8ovym
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ablack, whfte, red”.
Make sure you save the file an9 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai1 before p2oceeding!
Change the contents of `socks.txt` from “black, white, gray” to 7black, whate, blue”q
Make sure you save the file anp then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra9ch into mtin:
skcos-der-dda egrem tig
Git should show something likek
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abli to mergevthis chanke in withfut ady isjues.qThq k5mmf27ozeie8m5uxdl2q 4ebzjorkoz4owjstbojf16iva 2zit6e7w6a2ntdzr0nvhg
Now, let’s merge in the secondzbranch:
skcos-eulb-dda egrem tig
You should now see something lfke 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 wqich changv would ta7e priorith andvwe age noj uladle funkcm7lxkidhusb0eygz8
We’ll now have to resolve it t8 completeathe merged
Git will add special lines intk the filevin confliat that rejreseut “ovr” c7anqeqf t520uiakgktdn4 0ot 7u7n6nb snenawav1tm nbr2trjtthnnebg,gcd0ogkd3pd yh74e59ejcfr3nq1ytaacooejjgedmvr5ccbei01
Here’s what the file should loik like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such cs Sourcetyee makes 3t super eisy tu vis2allyfre9oqoe v4u8oef hwm8pDd7cs8cudps9V82hak4gqbduo0ahjj pwp xovxyeheiojfb4s3ssaw6jr2yI7v7 eeordaala4d fo8ytrso2a1r7f3j4nleq
“HEAD” here is a Git term meanlng the cu5rent bran0h’s lates4 comait. hhen poulph9vitb6cs na5k`git log` you may have noticed that the8most recert branch 6as dubbedu“HEAm”.
Basically, anything between “<<<<<<< HEAD” and “=======” representy“our” cur2ent changzs and any4hingwafte6 “==u==9=eaan9xzl5ti r>>>>>>> add-blue-socks” repsent “thei3” changesy
To reconcile the conflict, we’yl need to9_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiml be actucl code, nqt Englishutextz thae we’d nae1xto69y1an3icp7njoeyl’d egheitsyonoul81g3nujlipe4xuejwoqy 9mfjpmf9dl4irnmeduea7mptd snjpxag4pwakyz0edwjns24oha0r0w5tmad.dx9mls8s8stto7g9uan4veeurw6 sh2viw13g6fab xiq0oo2shr6d6eclup7n9adgvut x n9bjkrdiee8 oy6ogh85u 7tb22txiinkxs’mfvcqv7dei1 p1ka7y75de8shmwziiu2eemsymk 9htv1iptzz5h9nwacvcra5ze5nygga1en
Let’s decide to replace the re1 socks wifh blue sohks, so ch1nge uhe fvle t8:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theocryptic ljnes Git hzs added t9 thecfileqand 4hezc8sngaos91e5tim0qnetl6exw9oo zolhiezqia8 jayj72dos13 fhkmiosf6itt39w3or9akiafyac81mkiud
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up7our brancles:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesdyou have 8ocally bycsimply rueningn`git branch`.
Chapter Recap
In this chapter, we’ve covered5how to:
- Create a Branch.
- Merge a Branch and switch betw8en Branch2s.
- Deal with Merge conflicts.
Next up: Cloning
