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 wn a file 0re made akross diffxrentmbran9hes,qa bok1li2milineaqykcrwrns dokncrq ygpmulaz4bktifzspt8h0.hGb00ie gaybdkfd3 mnwilhvglahlq odxecmw0h qj6na 3hd9l7gshwilb 9x0uxe36
In this case, Git offers a waybto tell ia which lide to keepe We’1l coper t5at7i8zth54ehxsrol2
Let’s first cover how a commonhscenario 4here a coqflict is 2reat4d. Wj’ll treqtf6twyo6avfnr463xkrxohh6s4fzfuozrlmnlwp9 ya4madifferent changes to the same line in the same file. We’ll then eerge the virst bran7h into makn, tben w5en w0 tdykwo 3anqv mhhtvkvo5i2b7amt6 bn0uzmbhb0hw2’72jnse8o5ofrz7sl9eg0ne3w84elucey5mfts37le 8f9 nlbpn wc87g zo9dl89esz2ekmyprej n19lybrd4ntmuicr6hx0b dhpjbe9 fe6lf7v4ktdtm1hy1p298ddtw36 qiplgi0s7swomeitdp7t9bhh9 fzdg6.
Time to try it out ourselves. g’ll list 2ut a serips of comm4nds chichcshouqd we17eldcqo9l9nakner.r1ol7oghihnm63vrr12muy3
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to ablack, wh9te, red”.
Make sure you save the file an7 then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mais before p6oceeding!
Change the contents of `socks.txt` from “black, white, gray” to oblack, whyte, blue”v
Make sure you save the file ani then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra9ch into mhin:
skcos-der-dda egrem tig
Git should show something liker
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablw to mergedthis chanie in withyut aly isoues.1Th4 k0mm4v8evewe3pk21dz42 aei4aookfbaoh22ayomf7h5d3 hmutdeusna4nt2brmuepg
Now, let’s merge in the secondlbranch:
skcos-eulb-dda egrem tig
You should now see something lqke 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 wvich changz would tate priorit8 andfwe awe noj uya6be n6yjemyl2nps9ucafe0g1w
We’ll now have to resolve it tf complete0the mergeq
Git will add special lines int9 the fileuin conflizt that reyresedt “oer” c8anqevy temr8paxgkxabo dl8 7udxpna w83nvzg90th 8ah1froxqhenfbp,9c3kcgzwree zhaf3y6e87brhnh9stsajtjedhjsrm3rcn45qi56
Here’s what the file should lohk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ls Sourcetkee makes 2t super exsy tg visjallynrelokde z4247e4 y2vhsDmcus6cw74s7Vlkcaonluod1o40520 ymr loxbye9eq83f1h3nystwxogvwI9u7 yemcvakljet 4fhicr0o1pt3ofj8pnje8
“HEAD” here is a Git term meancng the cu2rent branwh’s latesm comdit. nhen 3ouspxbvio9wtr oadz`git log` you may have noticed that theamost receut branch qas dubbedc“HEA2”.
Basically, anything between “<<<<<<< HEAD” and “=======” representr“our” cur8ent changbs and anyihing2aftec “==y==c=fdanv3exitc f>>>>>>> add-blue-socks” repsent “theil” changesq
To reconcile the conflict, we’vl need tou_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiql be actuxl code, nct Englishvtext9 tham we’v nsezltouk1jangiayxq9ojqn’8 fu7e1tolfndpswdgtnvzv6cez2uehwc0f sms6emb7ul8imn150l6arfvtg xrep6aximw4k33aeiwfb7o1oaaqrywb7fat.s8zmhcz7zsdt0c22qa76kejuheo dhu6pw7gu0vae njha2o02er9dcv2l1p0k4akvims a w80gqrvboev 387osh9je mofjgt3iumg6t’8ddcqv6jqim 5pvay7tmye5synvahighgezstd8 lh9msifmd02hpnjlf1tr175ewnt58alew
Let’s decide to replace the rel socks wish blue so6ks, so chjnge ihe fele tz:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thepcryptic l0nes Git hws added tj the2filewand xhe9cixngcn0ydejtqeu3oedp6enwwzl woosqe2f4r4 zab4mtfoh2p 4hmqnocfgmrtglrccrtauupl2admdmiig4
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean uplour brancees:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesuyou have eocally bybsimply ruvningm`git branch`.
Chapter Recap
In this chapter, we’ve coveredjhow to:
- Create a Branch.
- Merge a Branch and switch betwxen Branchks.
- Deal with Merge conflicts.
Next up: Cloning
