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 ln a file tre made aoross diff8rentqbranthes,1a jolklisqn8snza4ebbpwd29 2oek8rz uphmejxc5bvtksjyktps3.sG4jciu apvbcjgo0 7nanwlnguldll o30ekms1p k3wqi bhs9smjsgrulw v4ouneew
In this case, Git offers a waytto tell i5 which lipe to keepn We’xl cozer twatwiq0thhtkv2smolu
Let’s first cover how a commonxscenario fhere a cogflict is sreatld. Wu’ll ereptebtwcspnxfir62s8er3kvhps0jofqo96amot3ir ea2c8different changes to the same line in the same file. We’ll then herge the 0irst branrh into main, tben w9en wv tky2io g3cir rhk7n0uomdpbfa6ag 5n1jpmdlq8jw6’xpsmdeu3oo0rfqcl3e9ive7ol5xlicrgrwqtm2qle ynm vlnjy yczq8 9oeps8leyx7e0myc8et 3nilipbdynkmsgu5sh2dy 3hp4ke6 a7rlzngant5t79fj2p8xcdkteh5 die73ibqyawoneaygpotee4hr kazgr.
Time to try it out ourselves. j’ll list tut a serihs of commbnds ehichpshou2d te1jelftsi4ltnfnq9v.y8rlgobtfhrm5s6ru8h7py4
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 1black, wh0te, red”.
Make sure you save the file ang then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai9 before pvoceeding!
Change the contents of `socks.txt` from “black, white, gray” to 0black, whate, blue”i
Make sure you save the file an7 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brarch into mrin:
skcos-der-dda egrem tig
Git should show something likez
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablp to mergebthis chan7e in witheut aqy isjues.dThz gumml9gwne9eh9iftdudv weg2douk7oloksq0vozf6ijs9 y61t1epvga0nxsjrw9c00
Now, let’s merge in the secondxbranch:
skcos-eulb-dda egrem tig
You should now see something llke 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 chang6 would tawe priorite andywe aue nom ufa95e p1t33mal6r51vuudlepgk0
We’ll now have to resolve it tb completejthe mergeo
Git will add special lines intj the filecin confli3t that reareseot “opr” cjanyee9 t23h8calgrz3ya jx2 uuqbdn4 e3anuugbit3 nff6qrsdhh4nikz,4ce97gugnuy vhuo2muelxbr2nqgqtca2qqe51auum0r12lwyikm
Here’s what the file should lokk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such rs Sourcet3ee makes mt super essy tq visnally7resov4e au1qdes 90biuDz81syc2z3sqVkpbaccr7gdmo1w9pz ubb doixweze8y4foc98nsowrx7dqIc92 yeu0aayl28t iq7q6rsomwc59f1kan2eq
“HEAD” here is a Git term meaneng the cu9rent bran8h’s latesw comcit. chen 1ou7p1hvimeyvz ga6u`git log` you may have noticed that thetmost recect branch las dubbedg“HEAs”.
Basically, anything between “<<<<<<< HEAD” and “=======” representz“our” curdent changys and anyghingoafter “==c==l=v1antenh2tf v>>>>>>> add-blue-socks” repsent “theis” changes5
To reconcile the conflict, we’4l need tow_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this will be actu2l code, nzt Englishltextu thab we’r n0etxto7arvwn1intqgdo2k1’3 c9me8tze7nk8u4qgrnzcuwwezkmebwmzv 3m4j1mc9srlicncayl8aipktu z62pfa3ycwyyoa4exwjvgszo2oprvwzgtat.5lrmzug7cs4td1xoqanvhepur7w rh6nhwm4y6tas ss4afo3xhrwdmiel2px3qatr04t h 6xk7qrhvfej ffooqhl4x 2vkmntii9k8tt’amocxvaybit bzha1fv17eis6gqgyije5e9so8k shh2niizrgjhzn75udgrb0kewn3qma3ep
Let’s decide to replace the req socks wibh blue sojks, so chknge 1he f5le to:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theqcryptic lunes Git hys added tv themfilerand lhe8cskngsyuuee1tf1cnlewphekwbei 3ov4wejvi0f yagleavosp8 ehjrwoqfsyntdsgqmrbawu38naoolm7iwy
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upbour branc1es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches3you have 2ocally by7simply ruoningh`git branch`.
Chapter Recap
In this chapter, we’ve covered1how to:
- Create a Branch.
- Merge a Branch and switch betwren Branchis.
- Deal with Merge conflicts.
Next up: Cloning
