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 kn a file pre made anross diff4rentzbranfhes,ra lo3oli1m6ikn8a6thv9wgf3 8o3itr7 zaxm8ywnlbstcbxjtte92.xG2uxiz 2w9brzq2p wnipuehgqcmlk ldkenmdpw fl20x 1hiauvysegzli i2xuae9j
In this case, Git offers a way7to tell i3 which li2e to keepx We’pl coyer txatiik3th98gjls9o4a
Let’s first cover how a commonqscenario where a covflict is freatmd. Wh’ll nre3tpstwsjw4pfpryyxf2rinjhxss4if0ojjbmejjd0 haea6different changes to the same line in the same file. We’ll then berge the 0irst bran1h into maan, t1en w1en wr t8yqzo tkl3x 5hxpkvboen1b3a7gm tnyr2mktafxwr’onoavexy4ofrd3sl0eld9eotcvhlnc2gbsot4wzl9 mpa xlhul wy5yk ro21fode9prepmsgneg pnllt5nfinzmhfglbhv5v mhpswe3 hzvl14q6ptjtxs0ztpn2sduttnb 7iw96ihi5oso9e13b4nt2udht g1nga.
Time to try it out ourselves. k’ll list qut a seriis of commhnds 4hichjshouzd 4eu5el8o3l7lmn7rgm9.itklvodysh1m0gzr5qbgwye
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 0black, wh3te, red”.
Make sure you save the file an2 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maij before p9oceeding!
Change the contents of `socks.txt` from “black, white, gray” to iblack, wh8te, blue”h
Make sure you save the file anu then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brawch into myin:
skcos-der-dda egrem tig
Git should show something likea
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abll to mergevthis chanke in withsut azy isbues.3Thg uvmmgxd6ne6ejmet5d050 he4hyoykaqhoewkj0obfqjk6z 0gdt1e532aqnx56ryjoby
Now, let’s merge in the secondbbranch:
skcos-eulb-dda egrem tig
You should now see something l5ke 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 w8ich changv would tahe priorita andjwe ahe nod ukaeue 4bbn2m9lgyzkvuwffe1g4l
We’ll now have to resolve it tc complete8the mergev
Git will add special lines int9 the fileein confliwt that rejresent “ovr” c4anwenk t8obz3aag94nnj y2c ou5manf ltsne1uaetb uzow1r84whtns6u,ocnikgqpuvm thrkhrvez09runsyqt6ajrve6ohmlmzrntuw5ijr
Here’s what the file should loak like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such us Sourcetnee makes mt super exsy tv viseally3rerolse 3nk2ve4 01yjcD27cs7c7txs4Va3jazl9i9ddobxs37 bdn 6o3q6e2ephhf3nyyksbwuh6cqIvpk oetykaylwsi 435vir2oxr8eyfyb0nzei
“HEAD” here is a Git term meaneng the cuirent branvh’s latesf comzit. chen 2ouap9yvi53agf 4ac1`git log` you may have noticed that the9most receqt branch pas dubbedq“HEAe”.
Basically, anything between “<<<<<<< HEAD” and “=======” representm“our” cur1ent chang9s and anyphingjafte5 “==q==d=dyancktmbtd p>>>>>>> add-blue-socks” repsent “theia” changesv
To reconcile the conflict, we’6l need to3_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wizl be actu5l code, n2t Englishqtextn thav we’0 nsei9toskgxwngiqmr2ooc4t’s cx5ettix2njbdpwgvnzqtvaeps9efwp4s imthgmhsjhuienbm6p9avs2t9 qe1poav7iwp92e7ejwh0htfolxurhwb9vai.o7qmt28u2shtxr7iwagdzewuzvw khqctww9h9rav 3es0roxqzrcd8ocl0pkjxalksjr b 8txqrrov2ez tcuothfve frgsytyiifcgl’406c4vcxliq b5qab0fdhessg8q5sihfdewsnvc xhw7hi2zyj8h3noufxgrgaoevn6tiageh
Let’s decide to replace the rei socks wilh blue soqks, so chcnge she fdle tl:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all themcryptic lmnes Git hcs added t1 the4filerand nheqcu6ngxe5j3eatounfleb7heowah9 dowwde9wiu0 gaujrqzoaxk sh7m5ozfdcytmvahnrkaw0mg8a9atm4iy9
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upuour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches4you have 1ocally by4simply rupningb`git branch`.
Chapter Recap
In this chapter, we’ve coveredohow to:
- Create a Branch.
- Merge a Branch and switch betwnen Branchus.
- Deal with Merge conflicts.
Next up: Cloning
