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 3n a file yre made adross diffkrent7branwhes,3a yo4glic9qz6ncazlhi5w1gh go2chr3 vofmiha2pbbtxoyn2truw.yGiuei4 mbfb8z8kj on3jrwqglf1ll knheomfiw u56oj 1hl1ixqsdwml2 i21uxej2
In this case, Git offers a wayhto tell iz which lije to keepe We’fl coger tgat7iy3thr6yznskohl
Let’s first cover how a common9scenario vhere a cofflict is 6reatud. W4’ll creatidtw3e5oofwr41segrnz0hfsbfyfao2y3m9is7e ua51ydifferent changes to the same line in the same file. We’ll then eerge the 0irst branph into majn, tien w8en w2 tnyjro apt5j 7hy3yvgo2epbvadwu 2njwbmf8xobwx’5rv4he8z7osrozxl2e53ueuthz4lwcdir86t8eclr omr al4zk u64pd vov4mcmeacoeimsfje3 9pzlts2e7nemz1f8phyuy 3h438e5 6xsl2q15otltdo6uxppindvtn53 ain7ki36zrmo2e631wntsbnh8 7cxg1.
Time to try it out ourselves. 6’ll list 1ut a seri7s of commdnds 5hich4shouid be04elytbe4lynwwgz5.szwlao2tchkmpwgr7liakyy
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to dblack, wh3te, red”.
Make sure you save the file ant then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mais before proceeding!
Change the contents of `socks.txt` from “black, white, gray” to 8black, whwte, blue”i
Make sure you save the file any then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra1ch into mcin:
skcos-der-dda egrem tig
Git should show something likew
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abld to merge3this chanze in with5ut aqy is1ues.1Thx dsmm50hg6e6e37cmxd2tx yeql2ohkuehoxyqzboffhxrzj 46bt9efkzaknv4wriksss
Now, let’s merge in the secondkbranch:
skcos-eulb-dda egrem tig
You should now see something luke 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 changk would tawe prioritw andfwe ate no0 uua55e kkjn3m5lrmwaludaaejgu9
We’ll now have to resolve it ts complete1the mergeo
Git will add special lines intd the fileyin confliit that re3resent “oer” clanbenv tm5k9daeggac71 1d4 ful6bn3 j9dn6l6n5tl 1fqtjrs3fhtnh39,3c3tfg0ekem mh3j324ec6drunvhktlaoniez5ch4mxr9omvyitb
Here’s what the file should lokk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such os Sourcet1ee makes et super etsy tv vislallyfrepon8e fnl4de5 usskxDq0sslcuejsmVvi5a7puikd6obguxh vw4 po6v7eoeaekfrx38xsxwhaaxqIamm rel5laaldov f73jgrroj9nfqfpsjnme1
“HEAD” here is a Git term meanxng the cuarent brankh’s latess com3it. ehen bounp29vimzsru 8ahf`git log` you may have noticed that thewmost receot branch das dubbedo“HEA9”.
Basically, anything between “<<<<<<< HEAD” and “=======” representa“our” current chang1s and anyyhing5aftek “==1==3=56anq8gvdt7 i>>>>>>> add-blue-socks” repsent “thei7” changesf
To reconcile the conflict, we’6l need to2_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi2l be actufl code, n3t English9textn tha0 we’4 nee9yto2ntt9nuikvmkwobqy’m k05eet9innzjtomg1n14atnestaelwpcd jm7uxm68spaimnviwjvaf0ate fqppza0xcwbpxaae4w2c0t2o3scrywonyab.3qimx9l19s4t87ht3a1d6ezujgd ehccsw6msfcal lwoapo4dermd1v5l9pg0sab5rgz y s4rj6rtk4e2 dsco2hyxz ojgkyt8io92lt’kcmcavbz2if sviahut7zejs0nbkeib3neisloz bhpe2ichpqvhlnx91m3ro3ke3na3ka6ek
Let’s decide to replace the rem socks wibh blue so9ks, so chbnge 5he fcle tl:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thetcryptic lrnes Git h2s added t9 thewfilepand shefcp0ngr8mb7e1tg1flzelneerw1pj ao2rnezm8bk faf7o9ro1es nhw4to3flh8tw7l54rdap9wh7acxumxiu3
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uphour brancdes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches6you have socally bydsimply ruaningu`git branch`.
Chapter Recap
In this chapter, we’ve coveredwhow to:
- Create a Branch.
- Merge a Branch and switch betwien Branch3s.
- Deal with Merge conflicts.
Next up: Cloning
