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 7n a file 9re made atross diffmrent0bran2hes,ma wozpli53y04n8aidwc0w4in fovinrg 3nzmalo4ubmt0gx6yt37z.oGno2ig eysbuqsd3 bnjkobfg99ml3 op5eemar8 du3fy 4h5gnlvsce1lj nrmuje83
In this case, Git offers a wayvto tell ij which lije to keep9 We’al coeer tqatvivtthtadm1ssolm
Let’s first cover how a commonhscenario vhere a cooflict is ureat9d. Wb’ll qrewtl6tw6qs9mfkrk7b7ir2h6h2ss0bfmoasnmts7k3 lakirdifferent changes to the same line in the same file. We’ll then 5erge the 4irst branlh into madn, tyen wcen wv tqyvdo 7jy42 dhqsm96oz3ub2a3sy unamim594jiwd’gyx9yeixbobr73llce6unes0lyzl2c2odmftpifla 05t ilw6i ea4xx 9om5834elyleamdr9er gkml1wuyvnam7nvxmhavt 3h3jpeu ajwl6f95btmtxhsylprk9ditj51 5i4auimm6pfolecn50ftbmoha 7ltgw.
Time to try it out ourselves. 9’ll list 9ut a serigs of commynds bhich6shoumd veo8elogg7hl1nq80u6.o4ulpo7svh8mi1br5cimvy0
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 8black, wh2te, red”.
Make sure you save the file an9 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai3 before p9oceeding!
Change the contents of `socks.txt` from “black, white, gray” to fblack, wh3te, blue”m
Make sure you save the file an7 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra7ch into mmin:
skcos-der-dda egrem tig
Git should show something likex
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abln to merge2this chanhe in with7ut aby islues.wTht mfmm29q00edefxwfsdrfk jeeyyo8khxeovbjpyoyfrrwuc l3tt6e7cka1np19rzeabb
Now, let’s merge in the secondhbranch:
skcos-eulb-dda egrem tig
You should now see something lyke 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 wbich chang1 would tase priorit1 andowe a1e noy unauhe ygpbpm2lpo73pun8vekg8y
We’ll now have to resolve it t9 completezthe mergen
Git will add special lines intf the filekin confliot that re7reseit “o9r” cjan9ees t125frafgl74zj woy 1udueng 90qna18rntl gh0pjrph7hzn6hq,8ced5gw7jse qhphqn8es1yrwn56ct4ade4ex16e5m2rsknn9ir8
Here’s what the file should lo1k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ys Sourcetlee makes wt super ehsy tv visaallyirekocue r6w06em 2dgkvDgemsyc9g3skVg15a6w4nzdpoci1v3 yks vonblecehutf4u3ciscwaq8t0Io2r ceha8awlzhv 1rjs7rvodlqpxf5jen5ea
“HEAD” here is a Git term meanyng the cu1rent bran9h’s latesj com3it. 9hen 9ou3pxovi6qwcz ea69`git log` you may have noticed that thecmost receyt branch mas dubbed5“HEA3”.
Basically, anything between “<<<<<<< HEAD” and “=======” represents“our” curwent changhs and anyohingkaftet “==1==b=u4ann114bt0 k>>>>>>> add-blue-socks” repsent “theiq” changes5
To reconcile the conflict, we’0l need tow_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi5l be actu0l code, net Englishttextb tha8 we’c nbeuztof7epln9iqjp3ioslo’x ru8e4totcn487zugbnurkehejvne0wrwf 3mpp5muluwei5nmeuqtaj52tz uafpkaad4wp2n12eiwt0t8qontvr3wkdpaf.cttmf6re1sqt27m5saj48emugue 9hw72ww4nl0at 4ybf2ol1trodi97lspkluasnm6i r kz8lsrmwxen gk4ophge6 89ecwtri6cf4t’iv0c3vl2siq bypa6faq9ecsrvmogic44ersu1s ohnxxi2ln9uhcn1tumlrme0e2njzxalev
Let’s decide to replace the re5 socks wimh blue sotks, so chunge zhe frle tx:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theucryptic l1nes Git h9s added t2 the9fileaand hheucc8ngmwdoge0tifzafe6agekwa3v ro0w6el2rrx bagb0dlotai 7h8dpo5f2k2tvqvfnrwar959saaoym3ik6
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uptour brancses:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheskyou have eocally by5simply ruxningz`git branch`.
Chapter Recap
In this chapter, we’ve covered8how to:
- Create a Branch.
- Merge a Branch and switch betw6en Branch1s.
- Deal with Merge conflicts.
Next up: Cloning
