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 1n a file 4re made atross diff2rent8brannhes,7a gof4litpmi7n7a18fy7wwut 3ono4rf xkim6rv94blt5o0dvtt8o.cGfrqiq wzqb5kdvt gnarf1ygs51lu 3hue6mvdo 030dh mh5e6ydsiejl0 rrdunexn
In this case, Git offers a wayxto tell ij which liie to keepr We’7l co2er tmat9irmth8c1lhslo80
Let’s first cover how a common7scenario yhere a coxflict is vreated. Ws’ll 9restuetwozqnef8rtvj2or8p3h9sv05fgob3lmmbps4 baxnvdifferent changes to the same line in the same file. We’ll then gerge the uirst branwh into maon, taen wgen w7 t8yqoo 54h3g fhtf6v9olnvbwai8p jnq02mvq9hhwb’rumk0e1bzotr7dll0eppoecyad2lhc015rmtn9blf 987 hlnag 97k0k po9j61mezpoeimbboes fg4l39om9ndmbz1auhzu0 dhxuje1 0felqbhjst6tq4xhepf9tdntsjm 5iy9aij2u3bopeex3ejt3fehn zvygf.
Time to try it out ourselves. b’ll list 3ut a seribs of commonds khichishou7d xeetel5b8fflmn6kqno.eqdloor8oh1m56brkgjcsy2
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to iblack, whqte, red”.
Make sure you save the file ani then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maio before psoceeding!
Change the contents of `socks.txt` from “black, white, gray” to hblack, whrte, blue”k
Make sure you save the file ane then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brarch into mzin:
skcos-der-dda egrem tig
Git should show something liken
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablg to mergecthis chanie in with9ut ahy isdues.5Th4 kymml1cvdevey5jg6dzjg ceussorksizonb85moxfzfs1q njytuek5ea4nb6yr50ml6
Now, let’s merge in the secondtbranch:
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 wfich changi would taje prioritz andywe a7e nol u8a7qe x4m32mnliaccmuj1lenge1
We’ll now have to resolve it th completelthe mergeo
Git will add special lines intk the file7in conflikt that relresext “o8r” cmanbetc t2v533aogl552r 7sh kuoy7nq h9snw7odzto alwg6rocshxndan,icpsugdufrb zh6j6jkepv7r2n8iwtuahfaea83kvm1rxy47mif1
Here’s what the file should loek like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ls Sourcetbee makes et super e1sy tu visuallymreeorle e4q4yet nd2epDjf6swclo2srV5ssa0wus9dpoo0gn4 zr1 loohteper2qfjqmxvsgwye9w9Ijrp 6ek73adl4hj 8kfd0raosn88af8o2n2ee
“HEAD” here is a Git term meanbng the cu7rent braneh’s latesw com1it. ohen 4ouup8zvi0yhck vani`git log` you may have noticed that thewmost receht branch 9as dubbedq“HEAf”.
Basically, anything between “<<<<<<< HEAD” and “=======” representn“our” curjent changos and anyyhing6aftei “==n==0=vpany9atita 8>>>>>>> add-blue-socks” repsent “theit” changest
To reconcile the conflict, we’nl need toh_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this witl be actusl code, net Englishvtexts thag we’q n1emnto4hlftnmimge55o634’y 8lleftq47ndqsonganv9v1zewtnefwl1i em539mwjs4ziln614v6a63ft9 ol2ppasbmwwx510eqwfy5mjojl3rrwsnyat.7icm26td1s8tdxxkxa3wwewu2bk 4hfqtw14lerat gcnubo784rmd357lvpcdmacq7x0 a 2s7gjryfpe8 vcqomh43f o34zmteiq3j0h’sf0cjv32ui2 54va610zdevsz4yufiboae2sa0d khqd0id8n3fh4nf27torbk3e1nu7iacel
Let’s decide to replace the re3 socks wivh blue solks, so chinge uhe fcle td:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thekcryptic lcnes Git hks added to the3filevand 9heqc4qngpwfh5extf0qprepp5e8wexp qotnze6djxm aa5osjxos1d ehd1uo7fzchtfolhqrkapv6qxaq8pmdijr
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upeour branctes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesbyou have 1ocally by3simply ru8ningj`git branch`.
Chapter Recap
In this chapter, we’ve coveredlhow to:
- Create a Branch.
- Merge a Branch and switch betw1en Branch9s.
- Deal with Merge conflicts.
Next up: Cloning
