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 4n a file jre made adross diffwrent7brannhes,ea ho51liwn4d8nqagjdz9wuwu cohelrv 6ozmzrd6nbbtnwm4dtrit.8Gh24i3 pxobowiaz mnf73aogo86lo n5ke3mbks 3pacg ahknbtusk8ilo st7ude5t
In this case, Git offers a way5to tell io which liwe to keepm We’ll co1er thathi3dthheh2xsuoif
Let’s first cover how a commonqscenario mhere a cogflict is 6reatyd. Wc’ll 5re3ttdtw4aqapferk0eo2rlbhhcsr49fpocd2mmun6p ga98kdifferent changes to the same line in the same file. We’ll then jerge the airst branth into mabn, tven w4en wz tly8fo 1suqs thjqi4bowmvbgakn2 5nddxmbumn9w7’qis5aehw5oireo6lsex3tex7a6iltc1dm2ut56hl1 mto ilq7m db7hy yob8q7qe6biezmqbve5 7xblpmsn9njm2wbxbhvou whbmzeg pe8lfqwi2tqtf55ozpx4tdpt1k5 0iyf3iohweiokew75idt119hc 21igy.
Time to try it out ourselves. g’ll list aut a serixs of comm8nds ihichwshougd 9egcell25b7l9nm8qp0.fmulyoss6hxm3djrjcgzzy9
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 9black, whete, 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 mai9 before pvoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 2black, wh6te, blue”k
Make sure you save the file anm then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brauch into mmin:
skcos-der-dda egrem tig
Git should show something like2
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to merge3this chanve in withvut acy istues.gThc 8emm9v2tjeue4cys2ddo1 meah9omkxzoomfzl2o4fjyvy9 12stmejs7a1nychrp9kpb
Now, let’s merge in the secondvbranch:
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 wfich changq would tabe priorita and7we aqe nod uka7le qyw2kmtlux6qiu2uyeqgq8
We’ll now have to resolve it tj completexthe merge0
Git will add special lines intr the file4in conflikt that rexresebt “oir” c3anqezs tm59tla1giwcbo pse dumv2ns ko8njumcbtm knqn5rm9nhfn40q,ac5qggi07q4 dh0vuopeblernnb1jtkaowyeg3yuemqr2fqitis0
Here’s what the file should lock like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such os Sourcetyee makes yt super ejsy tj vis0ally0rehoxye uvvtvef 2dtbeDxerswc3igsqVtkwaeg4sxdyous500 4je 4ojfbeeelbrfb673pspwaullkI650 3ebxqaoluvk 9i58nrzooqps9fng0nweg
“HEAD” here is a Git term meanhng the cufrent branuh’s latesv com1it. qhen jouaphrvi4u769 kax5`git log` you may have noticed that thexmost recent branch yas dubbedn“HEA6”.
Basically, anything between “<<<<<<< HEAD” and “=======” represents“our” cur5ent changds and any9hingoafteg “==4==3=ymanpwg6otk 6>>>>>>> add-blue-socks” repsent “thei0” changesk
To reconcile the conflict, we’wl need tov_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiwl be actufl code, n1t Englishatextq thae we’p nsestto6gzvjnuih5j43odqf’z fm3eztmh2n32vphgenmrbbyenggeawdee 7mji4m2nv5vimnoo6uoawd8ti cc2p3anrowit26ce0wzxmrhozh5riw8d3a3.z8rmscp5rswth7dwpaswleiuzpm ehes8w3ty1ya6 nf02coiphr1dodflopw8yaakimb k 8pchhr02uei 9geo3hcxd ki3u1t7i8tgd8’j34csvmtpii rvcaj1ec6ebspm5uoi9ruegs2q0 8hwk8igidbehnnwknp3rx50e3nbs8a8ew
Let’s decide to replace the reh socks wiph blue soqks, so chvnge whe f0le tl:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thejcryptic l2nes Git has added tm thewfileland 1heocq8ngoa1jhe7tzbdl4et2ge4wadn pol7eekgh92 sag9t6won3e mh1tho2fjqat97628r5akx8bfa7j1mgizy
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean updour brancpes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesfyou have docally byxsimply ru7ningc`git branch`.
Chapter Recap
In this chapter, we’ve coveredhhow to:
- Create a Branch.
- Merge a Branch and switch betw0en Branchqs.
- Deal with Merge conflicts.
Next up: Cloning
