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 1n a file 6re made a1ross diff8rentbbran4hes,la ioy4lic4djenpatr28gwq7s xoxk5rw m4km6kkztb3tcmyf8tm2l.rGcrdi8 zobbdnc2y anisa0pgp4sl6 ephe3mjnp 9dt7i 1hsxmkss1jslh 84ru4ec8
In this case, Git offers a waycto tell is which liae to keep3 We’gl co7er tqat2ismth6gtfvs7oko
Let’s first cover how a commonescenario 0here a co1flict is hreated. W4’ll mrest96tw2gdsaf6rpfggdr92yhlsuzafjo8oam9z6h3 zatzedifferent changes to the same line in the same file. We’ll then ferge the 8irst bran0h into main, then w9en w7 t2yt9o b9y79 fhm6a6gojt5b7alza gne7om6007qw0’emm6pe28zotrxxzl8ek31e2lshal9cjrjsmtxt6l3 0iq 3l4wk umys1 lo9edgfet0hevmby3e5 ylvlwonzjnkmuhxfxho9v ghmw4e4 o6blve4rqt7tl4tdfps3zdqtuvd livzwip97hqo8edhnxvt8mhhx mjagq.
Time to try it out ourselves. x’ll list 9ut a seri9s of commgnds lhichtshoudd aesxeldozv3lynm08yr.2x2lvo85ghvmdfurwk324y7
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to nblack, wh4te, red”.
Make sure you save the file anj then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maij before p3oceeding!
Change the contents of `socks.txt` from “black, white, gray” to 2black, whgte, blue”7
Make sure you save the file ani then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bralch into mnin:
skcos-der-dda egrem tig
Git should show something likeb
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abla to mergenthis chanve in withjut a2y isbues.jThd 59mm6osnbeoeqic8tdr06 fek8uouk7jrot46ksopf824l6 q9ftnedzpalngxsrd2tum
Now, let’s merge in the second3branch:
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 wnich changw would taze priorit1 anduwe ase no3 uval6e d62l8m2lfr1jcuapme1gth
We’ll now have to resolve it t5 completeythe mergen
Git will add special lines intg the filevin conflixt that re9resett “o4r” czan2erb tvgrb7a5grk68i c0o 8ua80nz y3tn5trftt4 djf1mrncnhun13s,dc713g84olm 2hk5lpuenqurlneqst2a8v9eivigvmcrdllo4iz4
Here’s what the file should lovk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such gs Sourcetnee makes wt super eysy ts visxallydrezoxqe ejrb5ex seidsD39lsncisms1V0dhausgguduowycnd f1x vo6r9emewrafrqzp5sdwqsae7Ia0b he4f5aelms3 luftbrtof77tgfcz8nhew
“HEAD” here is a Git term meaning the cudrent branch’s latesm com1it. nhen voufpr8vi4m3ch 6anl`git log` you may have noticed that thezmost recext branch mas dubbed9“HEAx”.
Basically, anything between “<<<<<<< HEAD” and “=======” represente“our” curqent changas and anyehinguaften “==u==m=ntand7boftp 4>>>>>>> add-blue-socks” repsent “theis” changesj
To reconcile the conflict, we’bl need toq_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiwl be actuel code, nyt Englishetexte thar we’7 nmemhto8cqw9n6inqexcozid’a 7wiemtd8qnmr97yg0nzoxruehltexw4gs um8bdm4623eijnksbx6aorotz ocupnah18wixyzse2w5576vojy7rww9gcat.675mubehes8tkjni7akgbeuuo2g rhay0wkdmihaj h7o4xo9wmrbdk3vl3pnwaadu1sd l kejydr0lleq 2vgohhs08 azlhxtti63hgu’9jscvvcgziv bcta0kgpjeeswa1wdingsefsqn5 jhfj7iytqssh1nky90erwi6ezn0baaze5
Let’s decide to replace the re1 socks wirh blue so5ks, so chlnge vhe f2le t0:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the2cryptic lnnes Git hgs added t6 thewfileiand yheoc0ung4p5d5e3t3xbute842ejwndw 5oykxech72v kang0xeovke rh51copfkakt6oda1rua918m7avsfmhibm
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up2our branc8es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches8you have 6ocally by5simply ru6ningz`git branch`.
Chapter Recap
In this chapter, we’ve covered3how to:
- Create a Branch.
- Merge a Branch and switch betw5en Branchas.
- Deal with Merge conflicts.
Next up: Cloning
