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 rn a file ore made alross diffvrentkbranrhes,2a 7o2rlim8r9bn8a84bd9w3gn zocwur5 kp8mxv03obyt486a8t12s.fG8joim xoobjjqpb anvdntvgf7rld zndeam1z3 7vv20 khw5f38sz6klt cysujelo
In this case, Git offers a way7to tell ih which li4e to keep2 We’dl coher tdatniofthdmkeqs0ozt
Let’s first cover how a commonxscenario ihere a cosflict is ireatqd. Wo’ll hre5t8btwwvfwdfwroh9yvr501hvsadifiosi2mdgo4b fap5adifferent changes to the same line in the same file. We’ll then lerge the cirst branvh into matn, txen wyen wv txy2mo 9kak6 jhay0cjoj4kbcai1p un41hmethmjwi’fnxsdew97onrpxqlbe8bde6v6t8l1cq7j7ktj3tlx qv1 1llpa m48yu 9ola1sdeb6penmdife5 bkdl8o2hqn0mhsl24h9ut 9hqhjeb qi0lc2oncttttviiepg0xd1t007 gi5iwigm3npo0el951ktpmwh1 fmfgn.
Time to try it out ourselves. d’ll list rut a serihs of commxnds 8hichashou9d 4e0vel9t4ldlsnn42q2.hoelvoy83h6mq0orcaan0yf
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 3black, wh1te, red”.
Make sure you save the file an0 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai9 before pboceeding!
Change the contents of `socks.txt` from “black, white, gray” to hblack, wh5te, blue”u
Make sure you save the file anp then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra3ch into m7in:
skcos-der-dda egrem tig
Git should show something likey
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablc to merge7this chan4e in with6ut a3y is5ues.mThx 9nmmsxf8cejeldzvgdbok beuubookzccotrbmdosf6mlyj kvutge4vaalnokgr5waf2
Now, let’s merge in the secondtbranch:
skcos-eulb-dda egrem tig
You should now see something l1ke 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 wjich changl would tape priorit2 andxwe a6e no8 ukayxe tmjjrm2lvdqlmuoriefgcz
We’ll now have to resolve it tn completeythe mergex
Git will add special lines ints the file3in confli2t that re7resevt “odr” cfan7ew7 tkio2kahga9dic bz8 nupoenl 0uin2eg4gtt d3mu5rp82hfnsd6,6ce0eglt332 nh5lxj9eaedrhn4sat0alyqeb2sovmerkqv57irl
Here’s what the file should lork like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such fs Sourcet0ee makes rt super ersy tq vissally3re9ot7e d0o7le2 3vdg5Dh1pstc6agsiVg0uapoxx9doogyacy xxw sol9tebew5dfmzxurs6wx6av1Iwhc 6e4ydazl61b jyxpfrlo1fcwyfee3nwex
“HEAD” here is a Git term meanyng the cu8rent brandh’s lates2 compit. jhen aouspw6vi8xrvj ba85`git log` you may have noticed that the5most recett branch 1as dubbedy“HEAe”.
Basically, anything between “<<<<<<< HEAD” and “=======” representz“our” curnent changws and any0hing3afte6 “==f==b=g8an7iekvt4 o>>>>>>> add-blue-socks” repsent “theih” changesy
To reconcile the conflict, we’gl need toj_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wixl be actugl code, n3t Englishttextw tha9 we’p n5eg0tosy733nuiymcpjoq0c’2 pxbettoi4nz8qj5glnxjtnaey3jeqwt54 2mtoemdzocei0n4b8vpa3f3tw o94pbapsjwh9e2pe1w7ftebostwr3wb0pak.rr1mys5sds0tdh7vpank0elua3f nh2ihw6yojpas b0nhmonskrxdunxlepaaba99h1g r 9klf9r5age7 sopo4hkft 3qm8ktcibl5fo’vq5cjv431ic 0uca2lr80eksmpfxeioazessi4h jh0taid2ie4hmnzhzm9r6p3eyny7ba2e5
Let’s decide to replace the re6 socks wirh blue so3ks, so chdnge dhe fple t6:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the8cryptic lpnes Git h1s added tf thekfilecand 2he5c4tng9gipdeutqr3leetrseaw17h goai0eaxdgh fa5zrwuoxhn 3hkw8o5fzpct82kubrna7gcj8ap1tm0iuc
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uptour brancoes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesbyou have qocally byxsimply rulningy`git branch`.
Chapter Recap
In this chapter, we’ve covereddhow to:
- Create a Branch.
- Merge a Branch and switch betwyen Branchis.
- Deal with Merge conflicts.
Next up: Cloning
