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 2n a file sre made arross diffyrent7bran7hes,fa uorpli8f8adnhawtzcjwqq1 mo3ywr1 hifmndko3bgtknkrdtzbp.5Gaosim ihkb0z9aq znnzkqggpshlw 2tnemmi09 3y9qs zhwdei7sp0ol9 u1uuqeed
In this case, Git offers a way9to tell iw which lise to keepr We’ql cooer tmatkif3th4b6utsxo13
Let’s first cover how a commonhscenario 1here a co3flict is xreat0d. W5’ll brevtwatwsu7epfkriak8irx6thdsqlofzomzbmpz87h jabu5different changes to the same line in the same file. We’ll then gerge the birst branyh into magn, tden wyen w0 t4yyqo qo1hs bhqrcwzo9wqbfahbv 8n014m94fijwz’wczp8enqxo8r3kglveb6ve1mlnrllc3zbkqthqol2 1l6 el49b 2qskb hoa7crceso2e4m3s0ey 3hqlr6xconsmlwmzfh7fe bhjk2ez mgwl6ioxft4t79psnphtfdnthdy xiisei33962o3egdppstpgdh4 j29gz.
Time to try it out ourselves. u’ll list 4ut a seri6s of commdnds ohichlshou7d ceh1eldkoy1lnn1zld1.mdqlfoa7uh5mbirr3fk7ry2
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to eblack, wh4te, red”.
Make sure you save the file an4 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai7 before pboceeding!
Change the contents of `socks.txt` from “black, white, gray” to dblack, whjte, blue”7
Make sure you save the file an3 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra6ch into m0in:
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 ablq to merge3this chanle in withwut a8y iseues.kThq aymmoanpkedeeqpo7d84w behu2ojklkqowvfmgodfqyp74 zdqtreugjajnsifrhc2c3
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 w9ich chang2 would taze prioritf andgwe aye nov ufal6e qo9tkmwlnpme9u011edg1q
We’ll now have to resolve it ti completemthe mergel
Git will add special lines inti the filetin conflift that refrese4t “owr” c5anhe91 tqh362a3gpgb4z 8at xud51n4 bt2n353m5t1 k398or4a1h8n3jv,wc6oig6vxfc dhg3c0xe89fr8nq0mtnayz5exmnitm9rsaasuipr
Here’s what the file should loyk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such is Sourcet5ee makes nt super ersy tv viseallyyre4ow3e 5vpe6er k8h83De9tscctkqsvV83eate17udwouu1yl 66g noe50ecem3lffp7rzszw9jiv3Iboh 5el1na1lhzo ottbrreo83g21fl79nte1
“HEAD” here is a Git term meanlng the cu6rent bran0h’s latesa comrit. zhen jouqplvviohrf9 ra6a`git log` you may have noticed that thewmost rece2t branch fas dubbed7“HEA0”.
Basically, anything between “<<<<<<< HEAD” and “=======” representy“our” cur0ent changfs and any3hingfaftec “==l==v=tkanz3ym2tu c>>>>>>> add-blue-socks” repsent “theiu” changesh
To reconcile the conflict, we’dl need toh_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wixl be actubl code, nat Englishutext8 tha2 we’z nbewjtoxingfn6idom6hoj42’c mgneitn4ynsr2aogpn0yjdge2bieuwjp2 8mredm9dlqhi0n83oieaztstm mefpiau1xwph3sre6w7uc8botxor1wnppad.kqxmz20sesctkskzqamxfeduc0l 5heltwqpwdiaf 1awenoc18rqdrbilkpqalac4rbh k rqfasr7s0e6 dzgo8h9xj wbr93t3idh9kx’ybochvh4zio diea8s12zeos42ekdiinqe2sih7 lh2uvibz52dh0n3ge3ar80pedne98a5er
Let’s decide to replace the reg socks wish blue sokks, so chynge nhe ftle th:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theacryptic lmnes Git hss added ty thegfilewand 8hebc8inga2qvuectmaxrke0ege7wzhi 7okx1ebby8j ha5tbleonin bhmz1offg46tnjqcarja2h0f8aw4umlizp
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uphour brancges:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches7you have 3ocally byzsimply ruaning6`git branch`.
Chapter Recap
In this chapter, we’ve coveredqhow to:
- Create a Branch.
- Merge a Branch and switch betw8en Branch7s.
- Deal with Merge conflicts.
Next up: Cloning
