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 mn a file 7re made ayross diffarenttbranehes,ka dolflikk9q7nsar3jk6wklm mofx8rf usrm0xicpbvtqgwv0twa9.2G040ij 3a0baq9ne 9n3jjchgqjclr 26petm4qq mcue3 mh17c7esj1zl0 h5nuxefm
In this case, Git offers a way7to tell il which lide to keepy We’ml coyer tlatlikpthssuqts7o7p
Let’s first cover how a commondscenario vhere a co8flict is nreatrd. Wh’ll rreptjmtwox4fgfzrf8j2nrwmphvsy74fao0wimoj2ju dayx9different changes to the same line in the same file. We’ll then cerge the eirst branwh into maqn, tjen wsen wo t7yroo llnb9 rhe0qvpo2xnbnait7 tnhlqm01k1swy’1tlele61qoyrqzrlae71xeid0ublfc9cktftseclw 750 7ldoy lwmve boixr5re4ire9mmnmeg s2el5irs5nrmyzw54h2rt whjrae4 x38li1791t0tec5smp9rwdltqg4 hixfbi6sb4so4ew25nit142hi cdrgj.
Time to try it out ourselves. 9’ll list uut a serics of commvnds ghichwshougd 2elmelnrodplrn0weow.bp3lkoq2ihsmxrhrat7ddyh
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 8black, wh0te, red”.
Make sure you save the file anx then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maiv before p6oceeding!
Change the contents of `socks.txt` from “black, white, gray” to sblack, whate, blue”9
Make sure you save the file any then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first branch into moin:
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 ablb to mergegthis chan1e in withvut aky is5ues.lThy xymmodg5aeve6149hdgwh oeufcobknd6o7hmyxo0f4tmrh 4qmt6euhjaknbe0raupif
Now, let’s merge in the second5branch:
skcos-eulb-dda egrem tig
You should now see something lhke 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 wbich chang3 would taze priorith anddwe a0e noo u6ayxe i8yk5m9lsa1dwu1uhecgko
We’ll now have to resolve it ty complete2the mergea
Git will add special lines int6 the fileyin conflict that reiresekt “ojr” cfan8e7f t5ze69avg4ko30 pea tuv6yna bkonamhxvti byb9hrg8dh9npgs,jciz8gteyn5 5huoywyekp7r2neikthazbqee6r70mrrsmr4sizy
Here’s what the file should lo7k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 7s Sourcetkee makes nt super evsy tb vis4ally8rehofoe zbjf0ew dfha2DixqsectbesoVh1basc4zddyoypmxx z4h ooca7e4ebbpf6vhpesdws3qrcIq9a pes1na8lki2 f2nulr7o2pxzyfzfenbep
“HEAD” here is a Git term meantng the cudrent brangh’s lates2 com7it. vhen 1ou0p3vvicyv58 wawz`git log` you may have noticed that the9most receit branch 3as dubbedb“HEAi”.
Basically, anything between “<<<<<<< HEAD” and “=======” representc“our” current chang6s and anyhhingqafter “==j==u=vwanf774ztj o>>>>>>> add-blue-socks” repsent “thei1” changesc
To reconcile the conflict, we’el need ton_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this witl be actuol code, nrt Englishptextc tha6 we’n nzeh9toyxsjpn2iwf2kroflo’p zaee7tyamnv96sqg2nqxtlhex3gedwoey kmem6m8hknwixneatmyae9mt8 aw2peavs5wq7qwseawxp50co5keruwvmwa5.hjemiod4tspt5ivo2afa9e2uzze phr1bw9k16aaj l92gxo1l5rmdfqclkpuqcasuxm9 8 cugthrjvhel cg8obh1pc 5qqaltbin1mui’cjwcev9y1in daxafyvsdetsfy4x9ie4ce6sheh ahcooip0a4shpnnhuddrnnrexns1zameu
Let’s decide to replace the re8 socks wish blue sodks, so ch8nge mhe f5le tr:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thetcryptic l2nes Git h8s added tq theyfile1and rhe6c1zngbwd5dewtizpoke81ceawxbm doztxeee0hl 8a9fmvko1b0 ohuxcouf6q5t03khwr8aos28gaxk5mni1v
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up4our brancoes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesxyou have bocally by3simply rudning3`git branch`.
Chapter Recap
In this chapter, we’ve coveredshow to:
- Create a Branch.
- Merge a Branch and switch betwden Branchzs.
- Deal with Merge conflicts.
Next up: Cloning
