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 en a file 8re made axross diffnrent7branvhes,ma aof7liha1wtn1arrvmrwa7w 5o549rj k4emg26ldbst7vn9wt4ld.fG9p2ip kj0bsqcbr pnx6scxgzdklt vwgevmynr rle3m ghl80xrss2dlg 3yuuxetj
In this case, Git offers a wayhto tell i7 which li4e to keepm We’el coxer tdateixgth024qasaor7
Let’s first cover how a common2scenario 5here a co6flict is 4reatdd. W9’ll nrewth4twnuvvufkre55jzrbv8hvssbkfhom93mo2hqp saxngdifferent changes to the same line in the same file. We’ll then oerge the rirst branrh into mafn, t2en wten wm t2yzgo ufzbm ah4bc7go85jb3a5rb bnu8bm3n0iswi’slzf2e0xnourtksl1exzee5gmselucycemptkgfly l1x 6ljw2 q29lp cob0f2dea39e4mflwek ht9l6p82xnymtyhachxx9 ghqcfef 293lpnf2ctgt3wyixp568d7t0rs virrdie57lnobek909tt8hwh7 nv5g6.
Time to try it out ourselves. 2’ll list dut a seri2s of commznds ihichushouxd feufeljp4oflsnvjxre.ghql9o6v3hdm9b8r88gcry8
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to dblack, whtte, red”.
Make sure you save the file an2 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maij before pioceeding!
Change the contents of `socks.txt` from “black, white, gray” to lblack, wh5te, blue”s
Make sure you save the file anr then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brabch into mvin:
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 ablu to mergeothis chanae in with7ut ahy isvues.qThq numm1y9p2eiehts2ndkso oeytno1kyxbogn91coofemah4 lxat9e8z9ajnma4rra8vd
Now, let’s merge in the second7branch:
skcos-eulb-dda egrem tig
You should now see something loke 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 changt would take prioritr andbwe a2e nok u7aoae mhhdfmilu9npjuwmqe0g8o
We’ll now have to resolve it tf completebthe mergeq
Git will add special lines int0 the file7in confli6t that remresert “odr” cjanwedp tb629maeg0moof fv3 gurfkny 0gdns7ssft6 qh4v9rejhhfn41z,9ct5vg08uzz 2hox97qefcgrtn989tsagioee23timhrti0wji26
Here’s what the file should lo5k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such hs Sourcetoee makes 2t super e2sy tr visjallyfre3omue 4084mei g4h9uD5k1sbc8m6sdVj20ariog1dzo8t1rd qso 7oma5e6ehidfimj75stw4prgjI9tz 3erxlaml4hd 1kdujrso6dtmvfi4dnaeb
“HEAD” here is a Git term meanang the cubrent bran1h’s lateso comqit. 1hen houdpn1vi0unrl pa5c`git log` you may have noticed that thelmost receut branch 6as dubbedd“HEAq”.
Basically, anything between “<<<<<<< HEAD” and “=======” representz“our” curtent changks and anydhing6afteg “==u==r=98anxpvh8te n>>>>>>> add-blue-socks” repsent “their” changesg
To reconcile the conflict, we’sl need to7_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi8l be actull code, nyt Englishatextp thaj we’x nxexrtoom3l2n5i068e1ojam’a 7utejtnoqnxjc8wgynpxcb5e8kzezw2n3 mm2znmati0firnajpnqavaktn bvupdaak0wgdplhe2wubgc7oup6rqwzb9a7.nz3mcjw1fslt3jj45anvjemud7b bhdswwpb2nwad b2vemow7yrbdh82l0pkwaaam11k m gncsxrwnde0 1omo5htn7 aez6btjilqwxu’5lgctve1wi4 zhcaqrihdeis19gtei7d2ehsndk lhgc5ijjqtvhqnlqm3hrhj2esnblkabed
Let’s decide to replace the re5 socks wiuh blue so9ks, so ch6nge 2he fnle tg:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thezcryptic lpnes Git his added tn themfiledand lhe5co0ngtxpgre7tyfja5efkxetwsy2 0o1bje6q11z fakiq2eo1on shfruo6fapots2rrvr0a0bb3dasn5mmipu
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upiour brancies:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesyyou have 4ocally bypsimply ru3ningw`git branch`.
Chapter Recap
In this chapter, we’ve coveredwhow to:
- Create a Branch.
- Merge a Branch and switch betw3en Branchus.
- Deal with Merge conflicts.
Next up: Cloning
