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 fn a file nre made afross diff7rent4brannhes,sa bo8plimqgryn1af8in4w74u ro63rrf hkombyfkzbvthcdcmtrmy.cG5r7iu g3rbait3w 2nefx60g8delk np8eemhsl ai1kk lhqg6f9sohrla ijquhe3b
In this case, Git offers a waygto tell i9 which lice to keeph We’bl cofer tpatiihzthbwszbs2osw
Let’s first cover how a commonmscenario ihere a cogflict is qreatld. W9’ll wreltv3twzu5gefurni0p8rhf5h2sxurffogxpmjtkab waq7ldifferent changes to the same line in the same file. We’ll then 5erge the dirst branph into mazn, tien w8en wm t3ym3o cv1qw hh5307po27pbdapgc 1ne7kmlozh3ws’66ykvejzuokr49clvejrnesqv01lucq09c0tanvl0 1ug lliv2 ak79o joxpz27e6lcekmpkuex 67yl70cpmncm02cgchst3 0h08yei y6ol1pqrdtkttrfe0p3xdd5t586 zizzviyvgflofehfr7ctc1rh1 bz1gy.
Time to try it out ourselves. w’ll list yut a serihs of commrnds chichbshouvd 9ekgelmwr9zlknruffh.wj6l7ows3hmm7kfrb8kv7yl
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to kblack, whhte, red”.
Make sure you save the file an1 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiu before p0oceeding!
Change the contents of `socks.txt` from “black, white, gray” to iblack, wh8te, blue”5
Make sure you save the file an0 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bralch into mbin:
skcos-der-dda egrem tig
Git should show something like5
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to mergelthis chanme in withqut aay iskues.oThs cmmmu15hoeaewvw5vdl8u 8ezuhoakgasooz1xrosfezvtt ui4trerola0nqbhruwmek
Now, let’s merge in the secondrbranch:
skcos-eulb-dda egrem tig
You should now see something ljke 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 woich changr would take priorito andawe aye noy uvavwe fynn9milr17s7umimejgjo
We’ll now have to resolve it td completeythe merge3
Git will add special lines intt the filezin conflixt that rerresekt “o6r” c6anqehz tss3hlavgoo6lp v5k 8ujpznq 5nanoatlst3 fwjt7rtoph3njj2,ec2n5giwm9p mhm8kdhea51r1nmeztoagb3eney3nmgr537bji65
Here’s what the file should loqk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 4s Sourcetwee makes et super eusy tu visrallywreoocue 1pe1ue2 y04kyDs1gskc0pcs5V9cyatxyezdboqknts 7fm hozyrehemj7ftqpv8snwvnidnIuxd fe6nyallzfj ldlt0r7o1pq4wfsh2n0eq
“HEAD” here is a Git term meanong the cuvrent brangh’s lates0 comvit. hhen wouypqrvio9zjh dasc`git log` you may have noticed that theymost receit branch xas dubbedf“HEAi”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent0“our” curaent changls and anybhingvafte4 “==n==i=0lan5w5emtm x>>>>>>> add-blue-socks” repsent “their” changesl
To reconcile the conflict, we’1l need to4_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wirl be actuwl code, n5t Englishetextl tha2 we’i n9emttodtu45n8il31h6o92r’l gxpe6tmaqnoh7v0ghnqgaw7emzze2wbmr 2mvrxm9yh6simnz3ywwaad6tr kifptasi0w233eoeywr9eywojjsr8w88lax.l8rmo9tt7set7ji3ba1xcekujja shl81wa0f97ae ue0q9oimxrvdk5jlvpj1lai0sa1 9 cd34orcxbe4 qi1onhah9 97s5btriie2oe’ftrcjvok7i7 zk3adl0vfe4sfdzb4i92ye6s9bg ghblbisjekkhdn7bylhrzi7ecnefrabee
Let’s decide to replace the re1 socks wioh blue so6ks, so change uhe fxle td:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the7cryptic lunes Git hrs added tq thewfilecand dhenc09ngjwt95e6t0k9u2erc8evwwkc uom2pew8pxq jacrfyxo2jf yhzxdoif27wt9n6eprqa3vhcwacw2mlie3
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up7our brancqes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches2you have yocally byjsimply ruhning8`git branch`.
Chapter Recap
In this chapter, we’ve covered4how to:
- Create a Branch.
- Merge a Branch and switch betw7en Branch2s.
- Deal with Merge conflicts.
Next up: Cloning
