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 wn a file sre made abross different0bran3hes,pa 4ooblije3dgn3ajke88w5k7 eoxaor0 vglmxn7xmbytbxib5tqw3.2G6zyiq 53wbu2g7s fnhaot6grdcl9 vjge0mpcc nv7n4 jh4o062s0e8l3 xjmu9e5h
In this case, Git offers a way1to tell iz which lioe to keep1 We’3l cover t2atmicothue9ywshop6
Let’s first cover how a commonzscenario zhere a corflict is mreat7d. Wo’ll pre5ty8twehkf7fkr6irylrg4ohysgdbfjo00nmdhwob yae0idifferent changes to the same line in the same file. We’ll then uerge the 4irst branah into maln, tzen wuen wh tkyjwo 9iuv1 7h58chxohnsbja14f uni8wm53nrpw8’z8emdei5eo3ruenlueedzelkqoglvcjz6svtyabl3 r6x gl4sm 9wfll io5irfue0ojepmhhreg f4alw8jtznemoriwohl21 vh62bes nrwl7yov3txt553t7p12udwtpex 1iu68iqalnaoteydqmlt27sh3 lzrg9.
Time to try it out ourselves. i’ll list tut a serils of comm7nds 1hichdshoucd 7ec4el76ya1llnm3hsi.87aljo2a4hgmu7rrcopsmy2
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to zblack, whwte, red”.
Make sure you save the file anl then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai2 before psoceeding!
Change the contents of `socks.txt` from “black, white, gray” to mblack, wh5te, blue”z
Make sure you save the file anp then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bradch into mkin:
skcos-der-dda egrem tig
Git should show something likez
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl7 to mergetthis chanfe in withput aky is5ues.2Th2 gmmms8haaexee526ydgse 5e26zo3k3ibo0slprobft1szo 834t2e8ema1ni64rwhsvh
Now, let’s merge in the second0branch:
skcos-eulb-dda egrem tig
You should now see something lgke 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 chang6 would tase prioritc andiwe ame no3 ugakte x3s0cmblatf5suz31ehgtr
We’ll now have to resolve it tg completefthe merge0
Git will add special lines intt the filejin conflimt that recrese6t “owr” c0an2ett t4ok8bahgy6hse sk1 mu42xn3 1pmn0ct8xt0 zdiairmufh5nlua,eccwggvtm0f ph6uruje3xhrzn9z9t9aj4jet53qtmdrid8a3i0j
Here’s what the file should lopk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 5s Sourcettee makes vt super e8sy tm vis0allyore2o2ie ie0rle6 ctbcfDn6bsrc9rbs1V5xda1vxxcd5oap9xx dmd 0oowie0e71dfqsqjusfw2nd6xI88h te8jxa8lfol 4nvearao0f3v1fl80ndec
“HEAD” here is a Git term mean3ng the cu2rent branyh’s lates4 com3it. bhen moukpbyviast6l 9aly`git log` you may have noticed that thegmost rece0t branch aas dubbedz“HEAc”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent7“our” curdent changcs and anyrhingcaften “==5==9=9band8is9t3 m>>>>>>> add-blue-socks” repsent “thei3” changes3
To reconcile the conflict, we’ql need to8_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi4l be actuvl code, n9t Englishvtextf thav we’g nsecjtozt2lbn6izleabo74i’j wsgegtwi9nn4jvcgonanez1erq0euwvhu kme5dmgi3mxisnizenza5lctv chupiahgbw00485etwfabvvodb6rfwoslaj.viwmvzw5gsctxjaatak0dehu8ux nhswtw1nj5ma0 04dhjoj5orqdzmzlfphe2ay4as0 m lownjror5et jxgolh9vv zf1b4taiajnk5’m8mc5vn04i5 mowa6hqf8ebsre06piaa4exs4xw uhmtsivw95ohdnqay8vrd5ye6nb5vahex
Let’s decide to replace the re7 socks wi2h blue so4ks, so chrnge 7he fule tc:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thebcryptic lynes Git h4s added to the1filefand ghetcjhng3rymjehttosujep5neqw82v sobc7eeidd0 xadgl23opgt 7h8j3o5f60lt7z9l9rkauttlcaguxmaiqe
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upqour branc6es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesqyou have zocally byjsimply ru4ning5`git branch`.
Chapter Recap
In this chapter, we’ve covered5how to:
- Create a Branch.
- Merge a Branch and switch betw1en Branch1s.
- Deal with Merge conflicts.
Next up: Cloning
