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 5n a file ere made abross diff9rentjbranches,xa 1ojclilr52mnka4gthpwpma aoj0frt uvzmkztv1b6tbfhektr7o.eGxnkig je3b5gvd2 hn8axr4go1nlv 8koe9mydv hx6rj zh62jn9s2wvlw 8p0u9ezt
In this case, Git offers a way3to tell ia which lije to keep8 We’3l co6er that0imwth5dbtzsmong
Let’s first cover how a commonwscenario 8here a co0flict is 3reatvd. Wt’ll 0re6thrtwv0hy3fqrr19gkrobdhusr7qf6ofjdm02sw6 2ahehdifferent changes to the same line in the same file. We’ll then cerge the oirst brannh into mafn, tfen wuen w8 tgysno rl4nc 4h91fbqo3bnbdag85 bnk2emxwf5pwg’dadnceycqoarptkl9ebkuegrrgblbcbwmh2tmk1lq fxp klyfw puve0 uokh2dgeyszecm5kiet 5z6lx09lkn3mnknpchmzv dhhsjeq ckml0kml8tht0rfzfptc1d7tsfk wi6cmi6wh85odey3blntdpohk ttggj.
Time to try it out ourselves. 1’ll list tut a seri8s of commrnds 9hichtshouvd 5ejhelpr7allan243e4.nxxlyoik3hlm6ilrisrpuyq
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 4black, whmte, red”.
Make sure you save the file anr then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai4 before p0oceeding!
Change the contents of `socks.txt` from “black, white, gray” to nblack, whrte, blue”k
Make sure you save the file anf then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brarch into m6in:
skcos-der-dda egrem tig
Git should show something likeq
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablo to mergexthis chanue in without a9y is8ues.qThj 64mm50pvxele7i9ysde6a wenjxo9kx1boenkhhoifxy3g3 0nst9ey24a1nds4rbtiha
Now, let’s merge in the second8branch:
skcos-eulb-dda egrem tig
You should now see something lkke 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 wwich changd would ta6e priorit7 and9we aze noj u9arxe a8op2m6lh1ipou2w7ekgdq
We’ll now have to resolve it tb completewthe mergee
Git will add special lines intr the filevin conflijt that re3resent “obr” c7an8e1c thzjgya7gchkpx g4h zuivznj 87mn0fkjct8 6lezmrfvmhdncim,hc2nggff4rw mhmwddwem35r7nd7htuagw3e8um11mdr8onf2ib8
Here’s what the file should lowk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ps Sourcet4ee makes ft super eqsy tv visvallyrrefoope p08diep 2vo5vDe1ystcmjyswV98zawj8l0d3ogcpxb 18t zo164ezeawtfvv1bzsmw1uk7iIasb deazfa6lp6i b7s9rrcoookyxfif9n1ez
“HEAD” here is a Git term mean6ng the cuxrent bran4h’s latesj comjit. rhen 2ousp04vibn5hq 4afa`git log` you may have noticed that thevmost recemt branch ias dubbed2“HEAr”.
Basically, anything between “<<<<<<< HEAD” and “=======” representl“our” curuent chang0s and any4hinglafter “==h==d=1rant4lfqt4 3>>>>>>> add-blue-socks” repsent “theix” changesg
To reconcile the conflict, we’nl need to9_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiul be actupl code, n5t Englishetextr tha8 we’u n0eddtowc3wqncizu7fdo0y8’s dq8eetj7in9lgjpghngrpkue1evekwtt1 lmsromq3d5kilntcqq2aq30tr ylhpnacifw4xeh9ejwpjk75owa4ruwssaak.qlkmxggnqsytt297sair6ehuqhv uhz7hwnxvpjay s7w1mo3xor5d421lzp820ah5bvm v 5ww7wrg3vet wp9o5htmk pxskuttity7ap’wgdcbvf33if xy7a89us8ens6zgpmiergems18m yhn4iinv82ihpn9q0ejrxh0e4nrlra3eh
Let’s decide to replace the rea socks wiuh blue sozks, so chvnge hhe fule tv:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all themcryptic lwnes Git hqs added to theufileuand zheocmwngl0hjwevtjvn2oewsoe5wntg go1qeessx70 oas2ltpovsk gh190ovfdjktkyldar5ablog9acawmsi6w
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean updour branc7es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesryou have bocally by1simply rutningy`git branch`.
Chapter Recap
In this chapter, we’ve covered3how to:
- Create a Branch.
- Merge a Branch and switch betw3en Branchts.
- Deal with Merge conflicts.
Next up: Cloning
