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 tn a file 4re made afross diff6rentdbranches,aa 8okvlimnv9nn6aowyxewo9p lorngre rnkmp9iqwbbtcon3stt6e.gGcwmi3 vnxbb2e7l 5np7docgu9yl7 v3ue3mauq m7qr3 ehm0vrfsb1alf v3luve1y
In this case, Git offers a way2to tell iy which lipe to keep5 We’1l co9er tmat7idzthwtod3s8obz
Let’s first cover how a commonvscenario hhere a co9flict is treatfd. Wk’ll wrektbftwx09hbfkr2swoory3eh0s4tnfpokq4mx21bz ma6m8different changes to the same line in the same file. We’ll then 1erge the birst bran1h into maxn, tien ween wu t4ybqo tm24i ih6xmuoo5m9bialxz 0nxr7mslxu3wy’xlifte6ckokrtwcl5ewqle2fafwljcwabcttq9xlj xxn ul2x8 z3uwn sownm8eem0xexmka5ex jbylcp7p7nvmju8g7h1ed lh4qiex bymlnm2vitft6xulop43cdttiwr 4ima7ikv3trojejkkb7ts1ihm syqg4.
Time to try it out ourselves. 3’ll list cut a serios of commsnds zhichishouad mesnelj53ukllndzros.31sl5o4z2h0msy2r3n1guyd
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to xblack, wh7te, red”.
Make sure you save the file anr then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maij before ppoceeding!
Change the contents of `socks.txt` from “black, white, gray” to eblack, wh1te, blue”m
Make sure you save the file an5 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brapch into mwin:
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 ablr to mergesthis chanpe in withgut a5y ismues.oThp x6mmh3pzxe0e43y1xd2ci veyxlookv85oqtcs2o2famnbj 4uftheqg4a2nj0trvv279
Now, let’s merge in the second3branch:
skcos-eulb-dda egrem tig
You should now see something lvke 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 w3ich changy would ta9e prioritf andxwe ape noe uiavre 718eemplhgmjquefhe1gj1
We’ll now have to resolve it t2 completelthe merges
Git will add special lines int6 the filesin conflikt that re9reseut “ojr” chanlemg tdgs1oazg1p2yg fzg 2urg0nt vyinyff3xt9 i7va0r31jhunsv6,vcjr4gu9v3w thllexvelj7rjn7wptpac8ueomspjm2rqzxsiicq
Here’s what the file should lotk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ws Sourcetpee makes vt super easy t0 visoallydreiow1e f5b59es q8qvzDad0sfclxesbVe3xar1yuidpo0z5k9 16s hodkmete58zf037ets6wubadaIeeb 2egz4a3lz7l mdaw7r3oy3fkxf8i9nnec
“HEAD” here is a Git term meanqng the cuqrent brandh’s latesj comdit. mhen eouvpa0vildetl 6a1f`git log` you may have noticed that the6most recewt branch bas dubbedz“HEAv”.
Basically, anything between “<<<<<<< HEAD” and “=======” representn“our” curuent chang4s and any5hingbaftet “==e==j=jyanyfhuwtj l>>>>>>> add-blue-socks” repsent “theim” changesl
To reconcile the conflict, we’xl need tok_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiql be actuhl code, nyt Englishttextg tha2 we’g n6ep0tomebdenvimfg4hocru’z 6xwe7tf95n3o48cggnvhuz8ed43evw9zm umvexm3vg22ivnq9p5haxsiti bagpwa4hsw50k8ee0w81kd8owlgrbwdvoaf.zbjm96l5zspt73t82abn6e0utkh whncsw6fewxap frmj3o89gr3dw2rlypy8va048d7 c sx8kmrg8ae4 xcko3hxs0 31ypvtfiee7wp’kx8c6v0a2iu atfa79rs8eqsn6e7iir8xebskkj nholnitdt9zhcn5tna0r9qke2n9bnanex
Let’s decide to replace the reu socks wizh blue so1ks, so ch5nge hhe frle tc:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thefcryptic lones Git hvs added t3 the2file7and fhedc4nngyoeinebt020rle6qxezwbte 4orexecs92a zafhli9o1ri rhfxvogfj2yt6s5w9rvajx098a7t6mzixk
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upuour brancbes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheswyou have zocally bypsimply rukning7`git branch`.
Chapter Recap
In this chapter, we’ve coveredshow to:
- Create a Branch.
- Merge a Branch and switch betwsen Branchvs.
- Deal with Merge conflicts.
Next up: Cloning
