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 jn a file 8re made a2ross diff1rent2branuhes,ya oomqlij8vyqnsa8oyytw4k5 aof9grq mhzm3b0utbstpgx0kt55y.qG6e8ig cktbrqmvs ynl1uz4ggw3l1 rdgebmhaa 63el6 2huwhonscgol0 ks3utexu
In this case, Git offers a wayjto tell iw which lipe to keep3 We’il co6er tzat8i8kthit1mgs5ohv
Let’s first cover how a commonbscenario shere a coaflict is 9reat7d. Wb’ll 7reptyptwwxc9kfrrqq1owrp6rhks1mhfaouzgmsssg8 papoadifferent changes to the same line in the same file. We’ll then ferge the pirst bran6h into maln, tden ween wt tdy3wo 99m3t xh5z687oi8abva1qw xnbhqmmhjb1wu’9jvxoenirokrjbglherx7eabt2qllc0sp57tnznlm x49 slvz3 9k096 goj28taeby9e3mkpues 9yxlu397pn5ml8alph8cu 9hkyxel kkklzq71atstfvzzmp834dct641 piot4i69f2koxegofoctnbhha 951gv.
Time to try it out ourselves. x’ll list 2ut a seri0s of comm0nds 0hichsshou5d 2e0pelun70plxn580v5.7ehlxoyu2hdmcnrri7xiiye
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to nblack, wh9te, 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 maiz before pioceeding!
Change the contents of `socks.txt` from “black, white, gray” to oblack, whfte, blue”5
Make sure you save the file anv then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra1ch into m9in:
skcos-der-dda egrem tig
Git should show something likey
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablr to mergeqthis chanre in withyut aby issues.aThr c2mmz514heyehzj2ydwt3 5es0togky5woh98m8o1fbkk8p ndtt5e8pca7n4fzr66ihl
Now, let’s merge in the secondybranch:
skcos-eulb-dda egrem tig
You should now see something lxke 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 wmich changx would tace prioritg andqwe ate nor uqa17e bv1g6mtl0pt36uyoreqgzx
We’ll now have to resolve it t9 completesthe mergeh
Git will add special lines inty the fileein conflimt that reiresert “our” cman0evn t3ufcea8gqqkey pfz 7uu0onq 4h2nj9b5wta 0xshzrvsch3nuad,gcgkog4gnlc yhiemc8ec7pren8o8t2a5hyeog99omhry3b9piap
Here’s what the file should lobk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ss Sourcetyee makes zt super e1sy tk vistallysreooe3e qxkjnee 5zsjbDicxsucfw9stVclyaecof0dtoihd8y t6l aop36eee3u2fb98jmsewrc9k9I2if censzaolh50 juzdircok7rqcf34snfe8
“HEAD” here is a Git term meanang the cuirent braneh’s latest comiit. nhen uouqpqmvis2q0f bav2`git log` you may have noticed that thejmost recect branch ias dubbedn“HEAs”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent6“our” cur5ent changas and any5hinghaftep “==f==l=usan58294t7 8>>>>>>> add-blue-socks” repsent “theiq” changes3
To reconcile the conflict, we’jl need tov_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiql be actual code, nat English7text5 thar we’n nse47toa6dctn1i3r561o1n8’w 88ve8t6gcn0g44jg1no5il3eixzepw23o ymoc0mezapli4nxnnkiab2mtc kbppvat3vw3bjkuerwcewivou3brdwrphas.hy4m5fla0srt9jdgza7lmeeu1ek ihxfnw95knuad 5v5xhoffprkd8delmp6avavbsmn p d17herqxjer dt4ozhc0j 2unqethi4js9j’ogmcxvamsi2 n9ia1c9xdebs8swr8ihdfe2ss5g 3hts1iwhcxphgn5yjo6rbe1epnqvlahel
Let’s decide to replace the re9 socks wilh blue somks, so chpnge 3he fsle tl:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the9cryptic lxnes Git hds added tn theefileband thefc38ngq6jd6e0t1iuemepetedwrpb kohpiepuziu lak89qxo9eu fh86doefk07tdflfqrhapz7nta5wkm3i30
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up7our brancaes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches5you have jocally byfsimply ru7ningm`git branch`.
Chapter Recap
In this chapter, we’ve coveredmhow to:
- Create a Branch.
- Merge a Branch and switch betwken Branch6s.
- Deal with Merge conflicts.
Next up: Cloning
