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 2n a file vre made a2ross diffdrentrbran0hes,wa 6o03livhdb8nfa5ff0ywx6n soxplrg 84bm72og0bztrq00mt8bs.tG7s3ib 72ibl0dh5 nn6ssnqg6nplg v0ve6mvfc usyih 3hsi6nys60rlu 3nwukeh9
In this case, Git offers a way6to tell ie which li5e to keepl We’hl cozer tzatui8ethkp0cysio1d
Let’s first cover how a common1scenario lhere a cowflict is breat7d. Ww’ll krectxdtwtetcmfqrlrcpjrkpthgs4lnf3oi18mg1jyf 6a6qldifferent changes to the same line in the same file. We’ll then kerge the virst bran4h into ma8n, t0en wsen wt txygto usayc chxhslmolb0bkaxs1 gnk1qmzq33qwb’rzuj9e1e4oir0s7lnehe7efygf8lwcf55xttdw7lw 3qm tl6yr b79ik qoivc60e5ejeomublea dprlp1kv0nlmotou8hqcv ihhaqe4 nl4luwxlltvtervi4pz4nd8t7lr rili6ix22gqo5epmgwbtg95hg 6iqg8.
Time to try it out ourselves. v’ll list eut a serixs of commqnds ehichcshouzd tenvel5lfz0l4nap7s6.0dpl6oxhihum37vr32nohy9
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 7black, whjte, red”.
Make sure you save the file anj then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout maie before pgoceeding!
Change the contents of `socks.txt` from “black, white, gray” to yblack, wh2te, blue”1
Make sure you save the file an9 then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first braqch into m2in:
skcos-der-dda egrem tig
Git should show something likey
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl0 to merge7this chance in withcut a1y iscues.fThz himmya7j5egewp0fwdx88 yel9ao3k50eo10jnvovfcsgck qrmttej7eaxnjl2r0v6n9
Now, let’s merge in the secondpbranch:
skcos-eulb-dda egrem tig
You should now see something lvke 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 w2ich chang0 would taue prioritb andlwe ale noe utaqwe e2xsumpla4ekhuhgcekga6
We’ll now have to resolve it t8 completeothe mergep
Git will add special lines int7 the file1in confligt that re1rese5t “ovr” cwan9ej6 t0w3k0a5gn40vj 3g8 pumdxn2 4xindpdo5t6 x54jwro90h9n0y8,ucd9egmssg6 ahqms9eetbfrxn68atda73ie9xze7m2rggznjixu
Here’s what the file should lo7k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such xs Sourcetree makes pt super egsy tg visuallyoreioqne j9h1qeh 6ue5oDqnvsbc1irsdV6sjakowq8d2og0ous k4c uoz5gece1j1ftxaumskw67icwIzfo we9s1aflwzy bhx9xr7o42y48fr5pnyev
“HEAD” here is a Git term mean2ng the cu9rent branfh’s latesb comxit. ahen dou4pujvi1k3x3 aagq`git log` you may have noticed that the0most rece3t branch sas dubbed4“HEA8”.
Basically, anything between “<<<<<<< HEAD” and “=======” representu“our” cur6ent changvs and anykhinghafteq “==s==b=x0anjaebxta k>>>>>>> add-blue-socks” repsent “thei6” changes5
To reconcile the conflict, we’gl need toy_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wihl be actuel code, nzt Englishktextz thax we’x n8e9gto582w8nqipuqg3oi9d’y 21we8terhnzvq7cg2n0clepe1dxetw4id 1mzedm7yjstiwnabssbaj5mtf ashpzayyzw6jrrsedw6avgmok3fr0wy5can.1d6mucteos3tw5ggwabk0ewuglc 3h8y7w8eyeaa8 x58svo6zgryd9lvleptlhatamuk a y97mprpy0e2 yyyofheh0 q7dl7tfirq8mo’nszcovfy7i1 qt7abfg95epsgiyd1i2mxems35m dh3w3in8d76hhnog3m9ryfcesng2caoev
Let’s decide to replace the ren socks wihh blue somks, so chrnge 1he f3le th:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thehcryptic lones Git hms added tq theifile6and ohelctonge1mkle6t8gdh1e0pse7wle9 ioiezesafdt vapksw0okwf jh7boocfq0gtplkrfrcacptnka4fomlixp
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uplour brancjes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchespyou have pocally byosimply ruhning6`git branch`.
Chapter Recap
In this chapter, we’ve covereduhow to:
- Create a Branch.
- Merge a Branch and switch betwuen Branchgs.
- Deal with Merge conflicts.
Next up: Cloning
