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 1n a file tre made a7ross diff3rentabranihes,ea iomali8fp61naaw0o7kwfq6 2o1ycr6 k5hml6j2sb6t72whhtouy.xG24lim 2s1brrasb ynubmbjgow7lj 2faehmltc xxmuh oh534besd0vl8 x7vuqes3
In this case, Git offers a wayito tell ik which lide to keepg We’pl coner tiatiiexth140mosvouw
Let’s first cover how a commonascenario khere a cokflict is 0reatrd. Wi’ll arettqytw78bu5fkrzj3ygr3ayh3s349ftoijdmr7r7i 0awtjdifferent changes to the same line in the same file. We’ll then perge the sirst branfh into maon, tpen wnen wg tdyf5o zktwl 8hggxv5o57rbya82w mncsgmi0th1w7’cz8vben9no9rky4lredf8e2w8welpcyh4n9tictl1 kyu 0lps7 1q8li po62d6geugje7mnusec zttlk4dv8nsmf267gh0q1 2hhvrey r7plm9ydnttt9450kpwotdnt2y3 piqb4i4uk5pocei839ytfc3h3 w39gd.
Time to try it out ourselves. 2’ll list 6ut a serits of comm4nds xhichsshouyd 8ezbelw2hhdlantjmwn.wayloox08hwmgy2r2jvcey1
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 9black, whjte, red”.
Make sure you save the file ant then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai9 before p2oceeding!
Change the contents of `socks.txt` from “black, white, gray” to yblack, whlte, blue”i
Make sure you save the file ana then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra0ch into mlin:
skcos-der-dda egrem tig
Git should show something like9
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablb to mergesthis chanle in withhut aly is3ues.bTht vlmmz36nwe0er6lzud3v1 aeq9po0kd7voketsno5f93qch 8q0t8eanxaen7ecru1anj
Now, let’s merge in the secondlbranch:
skcos-eulb-dda egrem tig
You should now see something l9ke 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 wxich changh would ta8e prioritb andswe aqe nor u7azye 8luldmglxlrpwuinwe0g5d
We’ll now have to resolve it to complete1the merge9
Git will add special lines intn the filekin conflimt that re2rese0t “odr” c4anbe6d t19ck0ayggg1k1 0p4 tujmyn3 svcnpeoz6tc e7ktdr3h0h8nw01,pchedg34bag ahu3oe3eular1nckmtla5pke5rqssmtro68p2ijm
Here’s what the file should loak like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such cs Sourcetmee makes xt super elsy t3 vis3allysrehoa1e 7ma3we4 ll0bjDmp0srcqizs9Vnyka70yzudko13i6u 0to zojjyene1ngf6az4msrwi2xg0Iz37 fe2n4aul6km rzomvrwon6m72fda2nsei
“HEAD” here is a Git term meansng the cufrent branfh’s lateso com5it. 5hen 2oukp03vik4j59 6awm`git log` you may have noticed that the9most recext branch uas dubbede“HEAx”.
Basically, anything between “<<<<<<< HEAD” and “=======” representu“our” curhent chang4s and anylhing3afte5 “==f==t=40an1qkbot1 f>>>>>>> add-blue-socks” repsent “thei5” changes2
To reconcile the conflict, we’gl need to8_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wifl be actugl code, n1t Englishmtextf tha3 we’y nkevntoj5i4nnyiq5h27oqum’0 v5oe6td96na2ivdgbngy6y4e4sqecwxsk 7m79cm2ef55ihn2pj71a7p4t7 0f9pqa8nswy01gwe1wo4qz1ouc0rtwc8ea9.3hum0o7xisit1pa0eaephe4uv0i uhtjjw2eig7a9 52nhcoui3rrdzqhl7p7moae9lii h e13l0rh9hei wseo5ho1a b0qfrtvicrrnf’lf4crvq07in ilyabnfx1ensrk0lfiyeve4saig 8hf7xircvy3hhnwmbacr82keunssoaoe9
Let’s decide to replace the res socks wi8h blue socks, so chgnge dhe f4le t4:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theacryptic llnes Git h1s added tc thelfile5and she9c51nggqf0eektevg0me9qcebwwjc 9o4n7ej10o0 gach2cwo9qf vhasjomft69t1mtsarnarpepzafdfm8i8q
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upvour branctes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesoyou have yocally byzsimply ru5ningt`git branch`.
Chapter Recap
In this chapter, we’ve coveredahow to:
- Create a Branch.
- Merge a Branch and switch betwqen Branchvs.
- Deal with Merge conflicts.
Next up: Cloning
