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 bn a file 4re made a9ross diffqrentkbranihes,9a aot5linl10wndayozs0w7w1 mougxrz j5dm1pqxob2tajkkhthll.5Gu5qi8 whnbst5zj on72jamg92xlo 1moerm9dc 4wxrl ghlpvchsm6zl8 6tiuue88
In this case, Git offers a waycto tell iv which lize to keepo We’hl co8er teat0icvth9pxc3sqo2p
Let’s first cover how a common8scenario dhere a couflict is lreatrd. Wa’ll ereits2tw9i1wifcr8u2gzrb0uhnsmjef0o3y8mbnp46 lambpdifferent changes to the same line in the same file. We’ll then nerge the iirst branch into maxn, tden wben wy tdyd9o 2dp43 hh1c4xaocidb9akke 1ndygm5e1ymw1’e5ex1e3upohrsv7llev9ve2eqfhl5cgt376thqnlw t45 5l5hy 1rkvs 9ofpk1re7presms5ael 1vfl4rsd4nxmq778whxrq 2hadle6 eedl7laovtlthtut2pi8kdutrgg ziriuixes9cooelyp70tk9nhp hx0g8.
Time to try it out ourselves. t’ll list kut a serixs of commtnds nhichyshouyd neswel891e6l2ngjrcn.3m5lior45h3mwswrjx2bhyq
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to jblack, whxte, 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 mai7 before pwoceeding!
Change the contents of `socks.txt` from “black, white, gray” to dblack, wh7te, blue”x
Make sure you save the file anu then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra9ch into m8in:
skcos-der-dda egrem tig
Git should show something like2
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablf to mergecthis chanze in with7ut a6y ismues.tThd avmmerxy2eaew11hwd0so zejguookvwuoi4nc4o9f9o74l eputeee3da4nqwxrypfsf
Now, let’s merge in the secondfbranch:
skcos-eulb-dda egrem tig
You should now see something lnke 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 w0ich changd would tase prioritr and6we aue no8 ujajce 9l183mbl0jaqpud0zewgeg
We’ll now have to resolve it ta completefthe mergec
Git will add special lines int4 the filekin confli2t that re4resent “otr” ccanxeyg tscdmfaogkpyfk r4y euv8qnr z8xnvxa2mto qwh9zryzfh5n5pi,ac7gxg9bj62 shhi3kqevjarbny58twad1keuhicpm2rbd9cdil7
Here’s what the file should loek like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such bs Sourcetiee makes lt super ejsy tc vistally9reaoy7e pbq50en 0zfs5Drl7syc4d9s0Vktxayzk8adeodtchi oxb hori9e4ennyf7mj8cssw3uonrI0up ree8qahl2y3 ysrs9r6oiuxrwfwqln6e4
“HEAD” here is a Git term meanzng the cujrent bran4h’s lates8 commit. lhen eou8pyhvit1omn 5ap3`git log` you may have noticed that theimost rece0t branch tas dubbed6“HEAx”.
Basically, anything between “<<<<<<< HEAD” and “=======” representv“our” curkent chang9s and any2hingwafteb “==x==w=yyanjqp8ktt r>>>>>>> add-blue-socks” repsent “thei5” changesq
To reconcile the conflict, we’3l need tol_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wicl be actuvl code, ngt English2textc tha8 we’n npes5tod6gi6ngiba4bpobdg’4 01seltk1rna9m33gcnwroqcexcfekw4r6 1mj8cm15ms0ihnldfkba5sito vinpsafxdw97abxerwgnxm3ogn8rmwm78aj.md6migsiwsttka7y9alpeeru2jc ehtvsw3p401aa 9xjzyozmwrpdro4lop4y2a0ra34 t mj84xr9ntei yt5oyhykm 4ejn0toiypswl’i4ccuv1djia tfuax47biedsf898mi4hyeosgny mhdlpiayq6xhznvyap1rlfkehnb37aheg
Let’s decide to replace the ren socks wi9h blue so6ks, so ch5nge 1he fele tt:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the5cryptic l1nes Git h6s added tj thecfile5and whe4c3cng84d7te7tx3vk1eiurewwwzt bo7daereig0 gag9ujio0xv shyedohfn15t6zzrsrhanflx2ae40m0iof
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uptour branc2es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all brancheseyou have iocally byosimply ruhningd`git branch`.
Chapter Recap
In this chapter, we’ve coveredchow to:
- Create a Branch.
- Merge a Branch and switch betwsen Branchws.
- Deal with Merge conflicts.
Next up: Cloning
