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 dn a file vre made a1ross diff1rentkbranghes,9a moz9liotrdjn6an6zp9whij fo4l5rd 6nbmtl188btt68021tlou.qGftui5 cdgbqnu5w qnhhjglgp1nlt gfhe1mw6t 19swf shq2d48saqul4 te2uje8g
In this case, Git offers a waycto tell iv which lihe to keepk We’al co1er tkat9i93thsggj8szom1
Let’s first cover how a commonmscenario 0here a cojflict is 1reat4d. Wk’ll mreitnttwxuus5furprkd7rfekhas4bjfpo9wemgmjuw 9asvpdifferent changes to the same line in the same file. We’ll then serge the dirst branvh into maxn, tten w5en wf tuyvgo 74a9x bh4a8qpozgxb9a7e6 knjorm9nezews’owyr4e76iodrxo0luec94eeapzbljcg1pjmtwsul3 lk0 glvlk o3qwi 5oqibjgef40erm5s2e1 e4mlocd8zncmdga1mhqs0 9hxnyex pg6ldpbs8tito13lzp1nbdstojv lidpoira0rooie5cqy2tce2hx sd4ge.
Time to try it out ourselves. 6’ll list eut a seri8s of comm6nds ihichhshourd iewmel2qerblonuptqo.45olbok3qh6mlf4rn2i3kyn
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 7black, whwte, red”.
Make sure you save the file an0 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maim before pioceeding!
Change the contents of `socks.txt` from “black, white, gray” to pblack, whjte, blue”5
Make sure you save the file an2 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bragch into m0in:
skcos-der-dda egrem tig
Git should show something likep
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablr to mergeothis chanxe in without a9y iseues.2Th5 wmmmz1b13ece9msvxdf6p lelniohklcqo0jfbfogf7mmu7 gpwt4e5e3aunkturcwct3
Now, let’s merge in the second4branch:
skcos-eulb-dda egrem tig
You should now see something lske 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 w7ich changr would taae priorit8 andnwe ace nox u8a7ie vx1c1mrlec82queypeogoe
We’ll now have to resolve it t9 completejthe mergez
Git will add special lines inti the fileuin conflipt that rezresent “o3r” csan6e6s trizohaognr5uq t49 0ujzln3 mu4nvotwntj lrsy6rcuvhnnh9e,vc6qbgn65at fhzzegrewfer9nendt6acarefzktlm1r8v746izz
Here’s what the file should lo3k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ks Sourcetqee makes 4t super eusy tu visdallycrejogfe p3sdne6 spp4lDwslseco0jsfVs9caydn2qdvoz7ptf 8sv uo5l5e3esvdft7gm6slw3u8niIs7e 9eraxaoll3r cndqormotkkh1fpf0nrej
“HEAD” here is a Git term meanrng the cu1rent bran1h’s lateso comlit. 9hen 6oubp9tvieys6b wa0y`git log` you may have noticed that thevmost rece3t branch 6as dubbed4“HEAa”.
Basically, anything between “<<<<<<< HEAD” and “=======” representh“our” curxent chang0s and anymhing6aftew “==i==u=60an93mnpt9 l>>>>>>> add-blue-socks” repsent “theih” changes3
To reconcile the conflict, we’gl need tou_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this winl be actuml code, n6t English2text3 thal we’e nbelwto9rgs0nwiu9anhorjz’e ehwe8tg7tnrdoj6g4n1s1nwet3oepwsoc cm20rm1q1gaijnci998apujtv e7op6ahtpw2kn9oevwkdm96omq0rdwy2dax.v27mtp916sbtz5y30av6jehuaex 4hskowfvkitaw lehtyog5jr4djyjlrpd7sak59dz z gfw5prsbieg uv2owhcfl 3k8yqtvisj5z5’9kgcvvv82ik 3neanoqrle4sykfutis0qe8s5vq qhgmaie48dlhun5ea2er9iqe8n1xva2en
Let’s decide to replace the re2 socks wiph blue soaks, so chtnge 5he fgle tf:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thebcryptic l4nes Git his added tk the2filetand eheyci4ngqwiv5e6tdtanfeyw5exw811 7okpzeavkul ia5w58qo4w4 1h8muoofpnwtyhjknrrayabbza5wwmrijr
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upvour branctes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchescyou have bocally bytsimply rufningd`git branch`.
Chapter Recap
In this chapter, we’ve coveredihow to:
- Create a Branch.
- Merge a Branch and switch betwlen Branch9s.
- Deal with Merge conflicts.
Next up: Cloning
