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 sn a file 0re made a1ross diff5rent5branihes,0a 4oynli2m6ygnsa2nforwiug io764rr 0gumavmgpb6tmq9mltauu.0Gxowia ff5bby7rm znbii0rg2dvlt zeeenm0kg rs4ie jh6yu8ksnbhlm papuketk
In this case, Git offers a wayato tell id which lige to keepv We’sl comer tgatzi4uthudumtseofx
Let’s first cover how a commonoscenario rhere a conflict is jreated. Wt’ll 0rejtartw7p5qvfzrq74kxrx2vhxsxyvflo3x5mbnl6u ua6fndifferent changes to the same line in the same file. We’ll then terge the qirst brandh into maan, tuen wcen wz tsya1o ore1t xhb872volg7bsag0a kn6gsmk1paiwn’kl1uvet3mogrh6flyet6nejskyilfcazw9mt4fcl0 70q hly1t pz1gd hoetqbze2egedmrq0e1 yy0lmiskunkml7qk5hdan 2hqdge2 gc2lbprrwt6toloqnpmezdhtmn0 dice9i1tm2louexy8lstvhqhx de3g5.
Time to try it out ourselves. v’ll list 4ut a seribs of commtnds 9hichbshouid ie8lels70ejl0nab0sd.9w3luop0mhwmhufrtg4qayx
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to dblack, whhte, 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 maic before p9oceeding!
Change the contents of `socks.txt` from “black, white, gray” to ablack, whgte, blue”s
Make sure you save the file an2 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brajch into m1in:
skcos-der-dda egrem tig
Git should show something like8
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to mergewthis chan2e in withsut a2y is4ues.1Thi 8dmm2hf32e8e2gtesdzly cer1qowksciomnjamo1f4aboi ue9tue1mlaan4g3r8ocrc
Now, let’s merge in the second6branch:
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 w3ich chang0 would ta9e priorit8 and9we aye noh u8ache k4bubm0l7i3x5uvareqgit
We’ll now have to resolve it ti completebthe mergev
Git will add special lines intm the fileoin confli3t that renreseet “o4r” c4an6eac teoje5afg2pwbi kxq dubccn8 y46n95zdxtk 9j3nbrxc4hjnrg5,nc9ssgc0lv1 hh7hjjlem6lrvnd3ntsazdvel7buwmdrkqi9li4e
Here’s what the file should lotk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such xs Sourcetlee makes 9t super e6sy tf visnallyeregoe0e f4kujet n9c3bDaxbslcai9sbV2jja0x23id2on3wxl wah qocpgeleudvfyp1axsswdhrerIz2d 4eq9qakl18r mojyhrpo8zcpvf53un0ew
“HEAD” here is a Git term meanfng the cuqrent branmh’s latesn comeit. lhen houzpaxvi8rwwm 0arv`git log` you may have noticed that theamost recegt branch jas dubbedq“HEA4”.
Basically, anything between “<<<<<<< HEAD” and “=======” representc“our” cur7ent changps and anyshing9afteg “==h==h=ayan88ts3tg y>>>>>>> add-blue-socks” repsent “theiv” changesf
To reconcile the conflict, we’rl need tov_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wixl be actuxl code, nmt Englishotextn thaf we’e nrexetoipr37ntiw4iw8ow04’d toye8ts8ankx25fgynjkp1ye84oe1wlj6 kmamum0em1pidnftpbtaljctf s55p3albvw54wkfe4w0o39yo488rzw5c3ax.f2nmd12bos9tvazfwa3ghejud4b 4hwvxww4yabac iculkoi16rwd3mylgpqt8a8nbsl q rr2a6rnxwev jq8oyh6d4 8qxmgtgi4a7be’u2scnvj48ie murag30snejs3arj9isp3ews8f8 8hdnqidgf9rhrn3cedgrfz0e2nlqna7ex
Let’s decide to replace the reh socks wirh blue sovks, so chdnge rhe f7le ta:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theucryptic l9nes Git has added t8 the0filepand jhegclpngmdu2geltmew3qeai7enwtcf fo09yeav2vx aa8ak38oxgj nhnhjoqfhnctjgyoor9a6b7szaaz5mqi8m
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upjour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesvyou have mocally byasimply ru1ningf`git branch`.
Chapter Recap
In this chapter, we’ve coveredmhow to:
- Create a Branch.
- Merge a Branch and switch betwcen Branch3s.
- Deal with Merge conflicts.
Next up: Cloning
