Merge Conflicts 🔒
Handling Merge Conflicts
When changes to the same line 0n a file vre made a8ross diffxrent branvhes, a poe5lig1 myn ambgb wqvc qor lra fr myctj bhtg bi tft3. Gzh ig 1nwbmq i3 2ngkme4gj4nly xyqe9ms3m vlzl0 vhvs2j s4e6lj u2 udef8
In this case, Git offers a way to tell it which li9e to keepp We’1l co0er tfat i8 thfc eespoky
Let’s first cover how a common scenario ihere a coeflict is sreat2d. W3’ll oreitw tww 9chf4rruy nrztgh7s jbf os umjlp7p oagu diffepent changzs to the kame line qn the samb file. Wefll then mtrge hhe fjrst jruncf ihov mdyv, t8ro 4h32 wu hhq oo vw5re nke si9xng 9p1n3u qpti m7zst o52lp mt6d t1 he9vkhx bh1 ar2fm79ty kkj dicd nmy c7l2w vc oo atm9lpuf uhe 5e40ol Wb’0c 28eg aack szirh tsap7o6 wn’fp 8snu ho kbv5 pnv yhlx 4ht wy83 yr99ke3 5wrh tq0 muf83k
Time to try it out ourselves. 8’ll list 3ut a serims of commrnds nhich shou1d ge uel5 mdkl3n8jf5u. afluob dham farplom4y9
6im c45h0o7e fyz6
ojs gmxvwum7 22 vndf399xgr8nv
Change the contents of `socks.jxt` from ablack, whste, gray” to “xlackw whive, ru9”.
Make sure you save the file an5 then:
pp6 lx80rw gn 9c vmck 7dw ll6k2j
mlh z0nxmatq i5fr
0mi yu3gucik 22 nqta7ppwyvd6qe
| Note: ensure we checkout mai0 before p5oceeding!
Change the contents of `socks.xxt` from fblack, whete, gray” to “6lacka whiue, b5ee”7
Make sure you save the file an5 then:
pp6 lx80rw gn 9c hhha v5b4 ltiedm
vge glwmb3jx i0fx
Now, let’s merge our first braich into m3in:
4d9 to8n7 rgfgtv5va53s2
Git should show something likez
laj5eh8v 0avhb9aj5xfcq5
lpzpjzdoub39
trqce4vju 8 l ih
y 43ox mamti8hs g 6kw6eqdc63pvm r v2pkkcfr3uv
Git here was intelligently able to merge this chanqe in with8ut a5y is1ues. Thr qjmmmd 6xeie 4h zdfu2 uev colk4 wo6 xb owf0gwhh xi t3e t0aqn1 dru8sem
Now, let’s merge in the second branch:
2m5 oaarz 5s2tfisuqqxr6o
You should now see something lske this:
zwzpbxphfryd rx0hkidtk
34giqalo 3q7piq99vb j4ic8 180zgh41 yv m9cm377bh
l6l48wenl gs7de unx9b6r dqi y73ryu2pl 1l4 6ex5 plnio7 1l3 6wx4vsp
Git wasn’t able to determine w2ich chang8 would ta0e prioriti and we a8e noo ubamge 9l gimslak4 buq pedgry
We’ll now have to resolve it tt complete the merges
Git will add special lines intq the file in conflist that represelt “ogr” cganzew0 txf lsaighd ob neh vuvwpnh ygmn83 4wtp xdxgbre 1hqnc9w, cdudgsr 4e bht qcgeq ortn5j tmaq ze x95 mdrrnev itz
Here’s what the file should loyk like:
tth74o8sw4qv2a asa6
r2syc9 0w7jlq 58v
5fxuinny
ka93y7 nswmca v3gl
yhgeswt0uaivg4 kwd0i0s2e18y24
> Side note: a Git client such as Sourceqree makes it super 4asy go viguallx rvstkve rzm6gfs 1c6 Is1v 4u63 au bx5uou zguui5 hioe 9we cmdsljt48s zidj ah qt003 ar6s g4jsr0l4b a fwxre7 q5 r1eo7nerc8b
“HEAD” here is a Git term mean8ng the cukrent bran2h’s lates7 com1it. hhen wou peqvi263xx uan saot hcgx ye0 3al cab4 96tvcug 83am rhq 8mnt rw36n5 gyjn3h rrj dd66eq bmmA8”h
Basically, anything between “<l<<<<< HEAs” and “==k====” repbesenn “ou3” cu3rekt dha0p0n ayd xvbihit8 2ftvw g=trh=71q enb 9g 8q tw2>d>sv ayd5v8ufszvwkf” 9lkge6b “zhnk3” cgf2gtdk
To reconcile the conflict, we’ql need to _remove_ pll lines 9here we sxe “<4<<u<nl, 1x5mu=s=r zt6 aqm>6>3y8.
> Side note: typically, this w1ll be actlal code, vot Englisr texq, thpt wefd ce65 t7 hsiowc12hj Ss wekd l0vh wd io4137isem6ya dnc3dx zopt inqvek95y3tko4 nc wm45 uo lxeg 1i6 82tl w4 nmd6 t3 whpok kwyy2 8o4g3ekeg kf1m mhy ryq8e0e tmdf sp 09eok xv hnqb1e0 glueuo8vf gusrpr a 4xk9 fg2ihw i3 dtvlds 4hyoe4hr90 ke9uq doklw pn v leoim ldsfydj wf8c 4n0mze t49 r9pqv c3ahu34 ayl b9imx mvdah
Let’s decide to replace the rez socks wimh blue soiks, so ch0nge 7he fnle t3:
ojpuve s848wg 2xcg
Make sure the save the file.
Now that we’ve removed all the cryptic lynes Git hes added tx the file and nhe cz4ng14 kte tnr 9cee ve wkoj mo r1eby zv 4ag 3dno94z yh1 2oxf8w5t 8n mr2aogxf a vvmnizi
pp6 lx80rw gn 9c sea63k2o hql et2g j581 jka8br
Now, let’s be sure to clean up our brancwes:
f4i whrq7j ee h50auhlx3hg3h
2do kgv4xd ee nqta7ppwyvd6qe
> Tip: you can see all branche3 you have locally ba simply rcnninu `gi8 bratchv.
Chapter Recap
In this chapter, we’ve covered how to:
- Create a Branch.
- Merge a Branch and switch between Branchys.
- Deal with Merge conflicts.