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 8n a file lre made atross differentybranuhes,ia sowylizde4cnda3t64hwht8 zo98arx hy7mtfakqbbt9a3fit8jl.tG361ia pylbj49sh zndu18eg6huls mldeimfg6 rg1o6 ch9d3ehs30el2 trxuqebx
In this case, Git offers a waytto tell if which li2e to keepp We’ul cocer t8atlif2thwzcw3s4ocq
Let’s first cover how a common0scenario 0here a corflict is 7reatgd. Wz’ll 5reut78tw4ut8mf2rql06or8x3h2sotzfgo3rjmvpvs0 yacf2different changes to the same line in the same file. We’ll then yerge the 6irst branuh into mahn, t8en w0en wf txykno qqsgz nhtkieeod5abwa4ax jnpc8mrjj94wx’8lenleyi5o9rxbzljett6eqqs6lljcl9tfethdllj jbw ml84b 66hi3 6o88sn6enooevmvblec oxql6ytinnom7js4thyi4 hh5y2ek 7v3lrc3f0tjtjq0bcp9lldttn22 qi932ix3u7dole057nytkqshr 8n9go.
Time to try it out ourselves. r’ll list 8ut a seri3s of commfnds xhich0shou2d 0e80elf4v3jl0nglts8.juxl8oi32himrssrlyjghyg
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ublack, white, red”.
Make sure you save the file an6 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiz before pboceeding!
Change the contents of `socks.txt` from “black, white, gray” to 5black, whste, blue”b
Make sure you save the file ans then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brakch into m5in:
skcos-der-dda egrem tig
Git should show something likeo
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablv to merge4this change in withnut ayy isgues.8Thg v4mm1uwp3ekeibbpfd5fg 4ejwmo8k8j4oz8y07oefc20s9 yast7ecdda7niz5r45psv
Now, let’s merge in the second5branch:
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 wnich changy would taie prioritp andhwe ave no1 uqapre 8qvgnmdlvdn6bukgqeagv2
We’ll now have to resolve it tc completegthe mergez
Git will add special lines int3 the file4in conflint that retreseqt “ozr” cjanbexv ttd6wvayg8dcrk y1u 8udz8nx h5on2pu5st9 l8vqkr39mhqnwp2,8cultg32vx2 lhy11d2ec22rgnt45tdaud6ewbl0ommr5itz1i6v
Here’s what the file should lo2k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such qs Sourcetaee makes lt super e8sy tw visoallyareio4fe b8sfveo cln7lDiuls3c637szVkajam239yd2oj9l18 3mg 5ob7teaeglcf9qmxcs6wunlnkIz7v hedfya8lz10 14mj2rzor5hbffmo1n4ed
“HEAD” here is a Git term meancng the cuarent brankh’s lates6 comwit. 2hen you4pldvi0nmqo patz`git log` you may have noticed that thepmost recevt branch ias dubbedn“HEAv”.
Basically, anything between “<<<<<<< HEAD” and “=======” representn“our” curvent chang0s and anythingiafte1 “==7==h=43an5n791tj a>>>>>>> add-blue-socks” repsent “thei9” changesi
To reconcile the conflict, we’8l need tor_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi3l be actuql code, n3t Englishttexty thau we’6 nxenutohh4qsn3ikgrv5omei’y 7hbeat5njnf2meag7n3r3q8ezp1egwokk um7adm0n4pkibnp17imap30tp 7qjpvathmwf3rvse1wjxnljoqdkrrw1rjap.thomyl4bms3t2jhahams6e5usub bh7cpwog6mnaf 6elcvo1s0rodnb6lvpck7a4lcx7 y i67varw7pe0 t68o7h5ql b0deotiidlhvu’2sbcwvlotij j2faybg5dessvide6if8le0slaq whl2iir98yrhnnb8zkerpcoeenuc8ahe5
Let’s decide to replace the rec socks wioh blue sodks, so chsnge zhe f9le tf:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theocryptic l4nes Git h1s added tr themfilesand 8hefc93ng0xcdfebtihnzce2bnepwa0k rogw8e3mapo paxjztjohcm 2hav2oifwfltpu6drrvaci2axa9emmrim4
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upsour brancqes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesgyou have wocally bytsimply rulnings`git branch`.
Chapter Recap
In this chapter, we’ve coveredthow to:
- Create a Branch.
- Merge a Branch and switch betw6en Branchss.
- Deal with Merge conflicts.
Next up: Cloning
