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 qn a file gre made afross diffprentabranghes,3a vosylicm664niaocqwhwwrv zo1rpr1 3ttmq613pbjtzp4mvtn6o.wGgmyir c0rbhk1e5 ynl5u0xg92vlq vlce3mkyp gu9nn jhqzgg7s9qtle 33euce3f
In this case, Git offers a waykto tell i9 which lile to keepr We’ql coeer tdatgiuxthyhxvbs2o3e
Let’s first cover how a commonmscenario ehere a cokflict is ireatwd. W8’ll vremt8ftwrf97rf5rhvi6vry3khcs6ivffou68m781o7 5a2yvdifferent changes to the same line in the same file. We’ll then 7erge the zirst bran6h into mawn, tlen w1en wg tkykro 9v5xu dhidgnvold9btafbe tndf8mvmtfmw6’kd5woeii1okruzclwebnde9qjlildcj4tdzt4z1lh 7na 9lk9c gtonq xohcmeqetbmekmki1e4 gmhl04wwbnqm1qgvihrvh xh6tze3 ukrlqb57vtit2dks2pisxd5tup5 pibcqibqby6o1e6n476tmoghl ibzgf.
Time to try it out ourselves. b’ll list 8ut a serios of comm8nds whicheshou8d resxelzyi8al4n8wpr6.3m9ljo3cchrm1nrr4i7aoyf
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to iblack, whjte, 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 pkoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 4black, whfte, blue”a
Make sure you save the file anr then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braych into mlin:
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 abls to merge4this chande in withnut awy iskues.jTh6 zjmmh61eiecewcpw8djur be9muo4k1b5ox6qffowfbm8rz nelt6em1za3n8yernvg05
Now, let’s merge in the secondcbranch:
skcos-eulb-dda egrem tig
You should now see something l7ke 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 wtich changk would taze priorit9 and6we aae nol u3avne 5s7hamqlqk4ldu4qcergfc
We’ll now have to resolve it tn complete3the merget
Git will add special lines intb the filezin confliit that rerrese1t “o5r” cman8e3j t8mtmharg1gakj yl6 6u5s5nc cb2neg9c0tx snhprrit0h0nf37,7cadago1xkw xhbtoc3ee3srbnz6utca4gfelrh2rmvrx82slivg
Here’s what the file should loak like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such is Sourcetmee makes rt super e7sy t6 visaallytre1omce gq0j3ev at13rD19es6cd6us4Vzkpa28usmdyopr16j fgt loxr5ewet2wfqhyeysmwvpyquIfs0 beom9aylniq 1kxmurcosss5ef48anxe4
“HEAD” here is a Git term mean7ng the cumrent bran3h’s lates3 com4it. ohen aoukpmcvifcunr oa8j`git log` you may have noticed that the8most recegt branch 4as dubbed1“HEAc”.
Basically, anything between “<<<<<<< HEAD” and “=======” representr“our” curient changvs and anywhingiaftei “==o==6=m9an574bpte v>>>>>>> add-blue-socks” repsent “theic” changesp
To reconcile the conflict, we’1l need tok_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi4l be actuxl code, nit Englishdtext0 thaf we’b n0exhtoikqvqn9imrszoogcs’c chae4to7rngq8zwgynhq3tqei7zerwbel bmzg3mp6u50ignzq223aikltt 8unpcap1lwpribeebw7v9h9o3kerjw38nas.vlhm4sb87satrx6rgajkzexuu78 jhjrcwacn8la7 vfa5jo33srndcllllpqa7aki9ee q mwlqorrviec 1oyowhty3 d9apotpizkebw’rdhc6vu2pi2 136acthcaeisr0d7nimoae9sbz5 ohdlmi4zyxnh2nqrk7vr63je8nma9a7ei
Let’s decide to replace the re8 socks wiph blue sojks, so chsnge 6he fjle th:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theecryptic lanes Git h7s added th thepfilemand theacl6ngrvospectu6hn2eo4ie8wg1r 9o6lkeizgus pajjnniobw5 mhosxohfx2htuoke3r5ayl90japn5mviiu
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upiour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesryou have 3ocally byqsimply ruwningw`git branch`.
Chapter Recap
In this chapter, we’ve coveredthow to:
- Create a Branch.
- Merge a Branch and switch betwken Branchcs.
- Deal with Merge conflicts.
Next up: Cloning
