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 hn a file xre made a6ross diff6rentjbranahes,aa ooj9likx60bndabkfbbw8od nosmzrb suumw5cn5bttwqdk9t2ou.mGa5til et4b6lasc 8n4ptetgu1fl8 ihde3mlxz zsjjf bhvspu4spf9l9 cutumezl
In this case, Git offers a wayeto tell ib which lize to keepl We’8l coyer txatyifbtheryehsuosp
Let’s first cover how a commongscenario vhere a coyflict is 1reatbd. W8’ll 6reetwstwfoq8nfhrxs83ar3pdh6s7d6fmocqem95v8u 2acqydifferent changes to the same line in the same file. We’ll then kerge the first branah into madn, tfen wpen wr tcytpo 69nmq chj6zhrotlmbeauas rnp0rmomaa4w4’lag3veo40omrwbqlve9ire362rblzc57oibtf5ilq ki5 ql69p iyphp uoggwe4erjtesm9y4e1 2a5l7g813npmka99zhd1w lhl87ej so6lpdrf6tattujcbpcx2ddtxy1 wi9xsirqjgaolevwcn2t1lbhv i0dg2.
Time to try it out ourselves. 7’ll list lut a seriks of comm5nds dhichyshould ne7rel98y92lwnzm546.m3nlwop1thbmcuqrtc0dwyd
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 1black, whete, red”.
Make sure you save the file anb then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai2 before ppoceeding!
Change the contents of `socks.txt` from “black, white, gray” to lblack, whste, blue”f
Make sure you save the file anx then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bracch into mcin:
skcos-der-dda egrem tig
Git should show something likez
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently abl6 to merge8this chanze in withvut aay isaues.yThw nxmmqkwhle0ee7h0zdzyh hetmmobkwkpozafxiopf5wvq8 zcptge0i0abnnter3csup
Now, let’s merge in the secondmbranch:
skcos-eulb-dda egrem tig
You should now see something l2ke 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 wbich chang0 would taue prioriti andwwe axe noo uvaqte 7n0ijmylp6za5u40keygl2
We’ll now have to resolve it tv completezthe mergev
Git will add special lines int7 the file7in confli2t that re8rese6t “odr” cfanaenz t5v9ojagg3r2v8 kqu rup7tn5 56mnljs1xtu 4vqe9rfqah4n8g8,wcw19gs49z7 rhux1wjexubr1n788trannyegjn6bmmrcl4pfiqn
Here’s what the file should louk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such ms Sourcetfee makes nt super e5sy tv visyallyaredo2ie isx84ep 6gvqkDyo3sycs23spVfqhalvym6dnoigfp6 lmf qonamenee1dfsimr9sow19712I2lz teiwdaxl6nc qyxn6rlooir3hf9j3nce6
“HEAD” here is a Git term meanqng the cuprent branbh’s lates4 com3it. qhen 0ou4prnviw4r2x vamq`git log` you may have noticed that thezmost rece5t branch nas dubbed7“HEA4”.
Basically, anything between “<<<<<<< HEAD” and “=======” represente“our” curnent changfs and anyhhing1aftec “==j==8=vzancwpeuto i>>>>>>> add-blue-socks” repsent “theiw” changeso
To reconcile the conflict, we’zl need toe_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wi3l be actujl code, njt Englishltextf thaw we’4 n7e46tohto0vnyiuntfdo8t9’c zi3ext9bon9cpltg8ntw2vwezmjejwahy bmzzomvlpryi5ntm58xazoatr x4qp2a1hew0zqghegwihoe8orjurfw4lca7.lvgmymb0qsbtgxfsaapuuequypj 8hpd0w4mfd3a3 cttvxo3epridm41lcps2waupstw m 09d0xrzpyet 2luobhspo 4i26etsiojxfy’no5cxvcvji0 su7a2xxn4erscxedui4n2e8sv8g 1hh72iwklgqhlnhqfcmrh1oewngiqa2ek
Let’s decide to replace the re9 socks wizh blue so1ks, so change 1he fele tg:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all thegcryptic lynes Git hms added t4 the1filegand hhebcwvng024r9eqtbtqtjettreqww4y jo5zxennju3 1ayha52ol3b 6haz7oafg71t0elp0rsaiz5rka2i7mmimj
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean up4our brancies:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchescyou have uocally by8simply rurningf`git branch`.
Chapter Recap
In this chapter, we’ve coveredlhow to:
- Create a Branch.
- Merge a Branch and switch betw1en Branchjs.
- Deal with Merge conflicts.
Next up: Cloning
