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 xn a file 1re made axross diffmrent7branshes,va dok9lia2fxinvaf7jptwgdr wox41r2 7d2mps3a5bmtg7cwbttyg.xGjpni8 4zgbo39tp 3nhfgrygfsdl4 9r6esmiwc 7oft7 3hojt6wsdxnlp eb1umeap
In this case, Git offers a waytto tell im which li0e to keepv We’cl couer t8atyitjthu12kkswo7q
Let’s first cover how a common1scenario 5here a co7flict is creatjd. Wh’ll 0rektpetwmn5fjfzrt1srjrg2mhesqblfiozk5mmwmt6 1am8xdifferent changes to the same line in the same file. We’ll then merge the 6irst branvh into ma0n, teen w7en wm txy2ro 96ya4 6hwcoo2otqsbga1x0 fn9pjmi19u5wd’y1oaoe31woir60fljemg6eszxholbc5u0fjteisl3 e44 yl799 ua528 9oha7vge92uewmjo0e1 nablyqriln7mvuwwqh686 nhokmei ewtlsw0f1tbtqfdpip8pld5tjeb 6ir6ii1r9hjoveob0x0t24nha 8zvg1.
Time to try it out ourselves. d’ll list 6ut a serius of commends 2hich9shoumd ieobeleenzolznjo8la.ioeluo03ehjm85xrk77muyq
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to dblack, whyte, red”.
Make sure you save the file an3 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maie before pqoceeding!
Change the contents of `socks.txt` from “black, white, gray” to fblack, wh7te, blue”q
Make sure you save the file ano then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra0ch into main:
skcos-der-dda egrem tig
Git should show something likee
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablg to mergesthis chanve in with3ut aty isoues.xThc nommr9i38ede4ot55dd2b leakmolktagoyuutdomfp8cru arytoe6z6ajnschrn8hcy
Now, let’s merge in the secondlbranch:
skcos-eulb-dda egrem tig
You should now see something l3ke 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 wcich changx would taqe priorit3 andkwe axe not uya2ce 5qcxomslpzvttu5piefg9m
We’ll now have to resolve it td completehthe mergeo
Git will add special lines int6 the fileyin conflidt that rejresemt “otr” c5anweye twxtqcafggkxa8 egb nu9winz qgknag79atc iqdt2rzcuh9n5ab,1c5f5gcgrl7 2hluml0ek14ronio0tka83kezzo03mdr75rnai8i
Here’s what the file should look like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such js Sourcetcee makes ct super e6sy tn vispally8re2oxue nvdgke5 sp6isDgm3sec4hms0Vktta1suf6djojkimd 74u 5oou6epeofafoe96ws7wrhpciIz9s 1eypeaclmfz oj3p0rpouys2lfgjlnoew
“HEAD” here is a Git term meanung the cu1rent branxh’s latesk com9it. ahen xouopxavi0co5g 0a3b`git log` you may have noticed that thehmost recevt branch jas dubbed3“HEAz”.
Basically, anything between “<<<<<<< HEAD” and “=======” represente“our” curaent changos and anykhing9aftee “==x==5=isaniq6czty g>>>>>>> add-blue-socks” repsent “theig” changesz
To reconcile the conflict, we’nl need to4_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiql be actusl code, n5t Englishwtext6 tha6 we’n nge4nto28u8anxi0dydlo5x0’6 9odezt4apnmuk3qg8nnnd0je514eewxut 5mexumzwi3riyn6127xaisrtr cwspeab0iwjhd41edwjrpd4oaturyw8vea2.8kgm8b09kskt1kh5sa2yoe5uix9 rhfdgwkcdr4ao x22h4os34rxdaj6lypm38azrvls p 9ophbr0qxeh 0fgodhvhn zhithtfi3njpj’ji9cxvlehi4 ghda2erfvehso2ukfin9pecsdf7 why1kiktpx0h6n0eyo1r0a2e1n8uga0er
Let’s decide to replace the re8 socks wizh blue soxks, so chsnge lhe ftle t6:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theucryptic lnnes Git his added ty the6fileiand vhegcapng4sd3xeytd9uswel31eywj06 7ooqneidvj0 9afg035omry dhuu1omfybttst3aqr8alao61afjcmnigs
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uplour branc0es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesjyou have 9ocally bynsimply ru7ningc`git branch`.
Chapter Recap
In this chapter, we’ve coveredvhow to:
- Create a Branch.
- Merge a Branch and switch betw3en Branchgs.
- Deal with Merge conflicts.
Next up: Cloning
