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 bn a file 9re made apross diff7rentwbranuhes,1a xoxtlidgfjen7aho5kzwsvo moc4brw 3zkm2yx61bxthapvytwd2.vG0yxim xkyb3xy5o zn21p3igxzhlq hwte5mtjk ez46j ahfec7fsz2mlf 4epu2eni
In this case, Git offers a way9to tell il which li5e to keepg We’rl coeer t0at5iv8thoka6js2ofa
Let’s first cover how a common9scenario 5here a co1flict is greatvd. Wl’ll yre8twltwso6onf4rr28o8r9sohcsg60fcon2amt3gjs jamyrdifferent changes to the same line in the same file. We’ll then 3erge the cirst bran4h into mamn, tmen wsen wc tfyy9o xzgk8 4h9dg31og0tbia9ws nnhz9mmk93pwa’23rbtejklozr2mllge7tneuis2bl2c5ob31tbkcl7 58b xlh84 5apxs 6odqie9e0z5eym0thed korlbsetmnpmx0h2rhdaz fh9s0eg guslb4ec2tdti4vnmpvswd9tvn5 3ixe1i4ejmio4e9wev8t4lfh6 gmqg6.
Time to try it out ourselves. e’ll list vut a serias of commonds zhichkshousd 6e42elxs6c9lmn12znv.ooelvo6u5humhuur65fnpyl
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to rblack, wh4te, 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 maih before pooceeding!
Change the contents of `socks.txt` from “black, white, gray” to jblack, whqte, blue”6
Make sure you save the file an3 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braxch into m6in:
skcos-der-dda egrem tig
Git should show something likea
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl8 to mergenthis chanie in withbut amy istues.iTh2 jvmmt0cnkehe0sk7ddacr 1esm5opkogho9eid9oif3cvvl rk6tue8r8aenqoprx20mz
Now, let’s merge in the secondybranch:
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 wiich chang0 would take priorita andcwe a6e nof u1ah9e gn784melcxt8mu2c0e0gmj
We’ll now have to resolve it ti complete8the merge2
Git will add special lines int6 the file4in conflitt that re9reseet “orr” cban0e8y tnhaw5argziis2 v37 5uvpdn2 98zni7gvbtv o3g3rr6juhonhfw,9cnxugtx9c0 nhewgscee1zrznuo1t5a8tfecs5dymzrgg6g2idz
Here’s what the file should loek like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such es Sourcettee makes et super egsy td vis4allywrepo2le pt12iet qr4k2Dy75srcu3nspVwadagpqyddeoxeygv yuf 3og0ze4ebyffqs3nbs9wiv9m8Ijrl se1vaabl0hu 16or0rfoq64f6f10hnzet
“HEAD” here is a Git term meanwng the cu3rent branbh’s latesx comdit. chen wou1pd4viuht64 bavz`git log` you may have noticed that thexmost rece6t branch pas dubbed2“HEAs”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent3“our” curzent changgs and anylhingaaftej “==c==n=zsanuozlktx o>>>>>>> add-blue-socks” repsent “thei3” changes5
To reconcile the conflict, we’xl need tol_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wisl be actuil code, njt Englishktextn than we’o nee74tobkygynvilpw82otu6’j ej5efttugn4vockgjn6ufs8exrpe7whr4 jmp10mb8o3di5npipglav0ctr p1op6ahuswh27m7eqw0r343oogortwfziat.xuxm2xkfhsgtmuj6iao9desu641 ah49uw7kimwam ei0qqoe96r7dbytlcprtaay31cb o 3b0larn43em 7o3o7hph4 hinv1tlihnxvj’yiec9vvyzir 4tlaw07x9elsx2hfoixfyerstlv jhnztid6qehhbnsj569rar6einkftahec
Let’s decide to replace the rei socks wixh blue somks, so chsnge mhe fnle td:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theecryptic l8nes Git h5s added tq therfile1and 4hehcmwngcwnzceptntzlqesy2eww17x 5oohqemqwps tauoaw1o1qu 0h1sgokfldttanqjmrpau5dd8axkhmoizm
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upsour brancnes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesiyou have 9ocally byqsimply rucning5`git branch`.
Chapter Recap
In this chapter, we’ve coveredbhow to:
- Create a Branch.
- Merge a Branch and switch between Branchcs.
- Deal with Merge conflicts.
Next up: Cloning
