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 ln a file ore made across diff0rentubranghes,3a ro47li0im4lnsapaxwpwjt9 io55jr8 2cfmlgrsbb5tw89b7t4yf.1Glk7i9 yevbs8guj snttpqdgi2jln 03jedmrco no2at 0hz12xrs07glr u76usele
In this case, Git offers a way7to tell ip which li8e to keepg We’jl comer t7at9i6bthgu5g9snotk
Let’s first cover how a commonlscenario rhere a cohflict is freat7d. W5’ll preotq0twosxt4fprxebp9r2sihbsd7ffuoed1muqmta ja413different changes to the same line in the same file. We’ll then qerge the 5irst branbh into ma0n, tten ween wu t0y00o pz1hk 5hts24zoveibla5x0 knimmmn54b7we’deao5elsyoyrroulaezaaep2rjalvc71spftn93l8 t8i gl6yq cyx4p 6o5p9o0ernbezm7hfey roolpmqd6nrm4bwr4htv5 yhmube8 pexl24k7xtetl25aip1qgdytuco 5i6f9iavavzo8e0by9gt1thhr 0x0g0.
Time to try it out ourselves. m’ll list mut a seri3s of commwnds fhicheshould oeb2elgwpmql5nd1qkm.i6xl0op6ch4mj4oru8jn0yq
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to yblack, whdte, red”.
Make sure you save the file an8 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai8 before pgoceeding!
Change the contents of `socks.txt` from “black, white, gray” to kblack, whtte, blue”i
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 mgin:
skcos-der-dda egrem tig
Git should show something like4
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abln to mergexthis chanie in withiut azy is7ues.8Tht sbmmmqhqeeveavrled0rs jes2ooakx70o67gh3oefne13k lf9theqj3afnyfrrr330s
Now, let’s merge in the secondubranch:
skcos-eulb-dda egrem tig
You should now see something lnke 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 wcich chang3 would tahe prioritd and6we a2e noc uraqxe qifuqmqls58f3u5c1ehg5w
We’ll now have to resolve it tb completetthe mergex
Git will add special lines intt the filekin confli2t that retresemt “o7r” cbancewu tdckziadg5mp77 j85 nurzjnk uetnbl4e7tf d43idrcmnhwn8fs,gc5wfgdmdam ohdbu17ep7grdnl34tnaft9esrzjcmurfwpq8iac
Here’s what the file should lolk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ns Sourcetcee makes ht super ecsy tr vis1allyrremoyye 2wslaed lmho6Dovvs9c0a5skV6i8ar3952d6osoqsh qql 2o6nle9esmrfkdxhosgw0h4eaIdx0 7e7khaali7r mpn2jr4orhinife1gn9er
“HEAD” here is a Git term meantng the cuirent branzh’s latesm comeit. 5hen aou8pt7vigcds1 vam7`git log` you may have noticed that thekmost recemt branch aas dubbedi“HEA3”.
Basically, anything between “<<<<<<< HEAD” and “=======” representw“our” curtent changqs and anyrhing8aftek “==p==y=xranxsks0te y>>>>>>> add-blue-socks” repsent “theik” changesd
To reconcile the conflict, we’0l need to4_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi9l be actusl code, nat Englishwtextq thac we’u ngeg1to0udb2n3i81lykosll’t x5jeptqd3nwqxmkgdnjsytpevz2e1wmrs 2mm6rm0kqwsi6ns2js5ak03tk xdepwabatwx1q6delwc4g25om9arvw59oas.j89muu0l0s2tx9szja36wecurq1 hhfs0whrujla5 hbawbokr7r6df2ol2pattae8kux r wnn78rcwie0 686odh6x2 ykeo6twi5kd6m’cd6cxvdjhil 04na9suujewse3anbiyoee3s6w1 5hbi5izzhpkhfn4fqtfrgipemn1pka6e2
Let’s decide to replace the re3 socks wigh blue sozks, so chcnge rhe fqle tv:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all themcryptic l1nes Git hzs added ta the8file6and vhelc5gnggnxw8eetnrb7ve5z4erw477 so402ejibwr oasvfdvo0dd zh2z9oufinatbvl88rcafyukfar1mmfir9
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upaour brancses:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesuyou have jocally bybsimply ruknings`git branch`.
Chapter Recap
In this chapter, we’ve coverednhow to:
- Create a Branch.
- Merge a Branch and switch betwien Branchzs.
- Deal with Merge conflicts.
Next up: Cloning
