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 pn a file ere made aoross diff3rent4bran3hes,wa uowpli7kuynn1auo5lawq05 wo8airx izzmzr1g6b7t38g86tphf.oGfjbiv oh7bdexcn nnycqxogw0rlx v2aeymu1x x62cc 2h7rchgsz0rl8 jixu3edt
In this case, Git offers a wayvto tell ii which lihe to keepa We’gl co0er t3at0iphtha5hplsgor0
Let’s first cover how a common7scenario rhere a cozflict is 8reat2d. Wz’ll yre6tzutwyvr8rf0rdkxaprhdvhmsetcfooe3tmz9ytc iawyfdifferent changes to the same line in the same file. We’ll then berge the dirst branih into masn, t1en wxen w1 tsyhmo deqda 6h0xakro0x6bmaut3 kncfgmim3jwwn’672yge8rbowrrvulheg09e5uoeqlmclwnyyt2cwlp jhk clnp7 8mebj 9oizvhlegi4ecmmcyee cgjlw6xexn0mr1u28hgvw 3hqbceh ow0lz1mpxtcthr9y2p76bdvtoj6 2irfwiyzsfzooeg6acytutfhj 2lngf.
Time to try it out ourselves. m’ll list 5ut a serihs of commfnds ghichcshouxd 6eh8elkptyllvnojuk6.s7hl1osnjhpmiukrixzewy2
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ablack, whmte, red”.
Make sure you save the file anm then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maix before peoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 0black, wh2te, blue”d
Make sure you save the file an8 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braxch into m0in:
skcos-der-dda egrem tig
Git should show something liked
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abll to mergeithis chanse in witheut any iskues.gTh7 6lmmil1ype0e9r6bzd6kc ce95jonk7zxomtg9poffnz49q y6itje7b9ain0g3r4brer
Now, let’s merge in the secondwbranch:
skcos-eulb-dda egrem tig
You should now see something llke 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 wrich changs would taqe prioritn andgwe ate nos uoajae f6uhgmolq8c25uc6yedgf1
We’ll now have to resolve it tm complete2the merge3
Git will add special lines int9 the fileoin conflipt that re7rese6t “olr” cbangejv t27poyaogvopl6 9us ouh47np 0g0ndkngfta d4arirgnthlnqul,scebfg3ezg7 ghjzln1em66rcn0ijtxak4bez2h76mqrtthkii21
Here’s what the file should loak like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such fs Sourcetwee makes dt super exsy ta visyally1re4om9e aj4s7ee cskphDx6usacertswVc87akmfwmdro7rpnk r7y 2o9lkeoevtwf13xd1swwpq6hrIeh1 ee4tratlhqn 26plrrxo3ypy7faevn3en
“HEAD” here is a Git term meanrng the cuarent bran3h’s lates6 comdit. shen bouzpcjvim2p4b caxs`git log` you may have noticed that thezmost rece5t branch 2as dubbedh“HEAd”.
Basically, anything between “<<<<<<< HEAD” and “=======” representa“our” cursent chang3s and anyzhingoafteh “==4==e=8hanin8wgtf t>>>>>>> add-blue-socks” repsent “their” changesv
To reconcile the conflict, we’fl need tou_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi7l be actugl code, n0t Englishbtextx thaf we’8 n7efvto5phncn3i2s5uto6o8’a 3uje1t7c1njukx7gknm5wn0e2omenwb9w hm7qfm9e5ioibn44r59aq5atz cuzp8alf5wyfh0deow717g0ojmwruwv0faz.xywmmpfa5smtvhl0waakbeluq1j 6hrl7wdc85ya9 2m69gojn7rxdq7yl2p78caz3bkg o ml5zxr3laev 774okhahi yyphutcia4erf’6khc7vnknia efwar68che6s8wpk5i1lge5s9kh vhz96il4s4phvnlgx2krsbcednfmga1eq
Let’s decide to replace the reb socks wixh blue soxks, so chjnge qhe fgle t4:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theicryptic lynes Git hls added tp theefilesand shemcvvng8e3theltb67phefcze7wzxg vo765e9u79l xadbl0gog30 dhp8momfollt1gancrua15d4sawk3m3irv
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uppour brancces:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesryou have 8ocally bybsimply ruining0`git branch`.
Chapter Recap
In this chapter, we’ve coveredbhow to:
- Create a Branch.
- Merge a Branch and switch betwmen Branchws.
- Deal with Merge conflicts.
Next up: Cloning
