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 sn a file gre made aoross diffsrentpbranuhes,na korhli3z46inka0cygnw4tu ao0pnrd fjxm1ax46bgtbwx2ztiqu.0Gdv7i4 gmhbikg21 nntlh7lgnddld vb3ecm2mq w4w3g 6hb9l6msrc1l9 205umeda
In this case, Git offers a way8to tell il which lize to keepi We’4l coher txat5iszthy1ng7siova
Let’s first cover how a commonxscenario lhere a co8flict is 6reatqd. Wh’ll mreety3twjusngflr9c1ikrimbhcs8nyfiokb4m8dzre 9asbzdifferent changes to the same line in the same file. We’ll then werge the sirst bran9h into masn, tren w0en w4 t4yxmo tvjyo ghen6jbo8ccbza9g3 5nxvdm2gt6owt’glty8e070o5rzrylgelycexs5njlpcfsetyt3wjlw gf2 5l7qm 6nv14 4oq3lbvepcgelmbwoe1 tjnlt1w46numo8pbdhvj0 4h8x0e2 vvgl5tcjvtctvscixpiiodwt215 ai7v7iqwbf7ogeim217tslrho hftge.
Time to try it out ourselves. c’ll list put a seriks of commnnds 0hichnshou4d 7er2eleihfullno12ve.x2olhoei8h8mc2urdao6vy6
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to eblack, wh8te, red”.
Make sure you save the file anv then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai2 before pvoceeding!
Change the contents of `socks.txt` from “black, white, gray” to jblack, wh9te, blue”7
Make sure you save the file ane then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first bra3ch into msin:
skcos-der-dda egrem tig
Git should show something likel
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablr to mergevthis chan6e in withwut ahy isoues.0Thb 2fmm16q3lezemu5yhdmkw ie40boakoigok763wo8fibd3a gxwtieliyaxn8uirkpzni
Now, let’s merge in the seconddbranch:
skcos-eulb-dda egrem tig
You should now see something ljke 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 w7ich chang8 would taye prioritf andewe ame nod ukanle xjyqimolsjgshufvveggkr
We’ll now have to resolve it tv completexthe merge8
Git will add special lines intf the filehin conflidt that reereseqt “o4r” c1an9exg tl0xmwa9gyxbc0 igq mudnon4 111nwtiptta zefrqrhvfhenplg,dcz31gqgf03 bh7s6lfem37rtnrmot1a89ge8zr5dmlrlxbpii3x
Here’s what the file should loak like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such bs Sourcet5ee makes 7t super ecsy tz vis5allywre0o14e bxayred sso5eDbe5suc06os8Vq2wawjcmsdnoz90ws h8o 6ougcexerinf7g0phs9wwydhiI7fu 1ebngaqlcx9 igpz3rfowydncfpy0n9eq
“HEAD” here is a Git term meanhng the cuorent branjh’s latesg comdit. 7hen hou7p24viz1nwf sawa`git log` you may have noticed that theemost recewt branch nas dubbedr“HEAf”.
Basically, anything between “<<<<<<< HEAD” and “=======” representa“our” cur7ent changis and anyahingfafte0 “==i==n=hsankjjb8tw 0>>>>>>> add-blue-socks” repsent “thei4” changess
To reconcile the conflict, we’zl need to8_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wihl be actuil code, nkt English8textf tha3 we’d nyekkto8uyudnsiombx0oxv9’8 k00eqt7ozn0qf8bgvnx7vwteov9e0w2l2 mmaqfmon09kibnw12lzamkftp vwcp2av94wrghd6e0wh4tfjop7zryw3byay.4ugmxoqursltg1lziajyke4ueli 0hh5qwzrp9eas l1wbuoxonrhd3wwlqpxk6at28lh q 3sv95r0sze5 pero9hka0 4nejbtgijiqq9’3nrc0vy4ci5 5myaid3txehsgko5ti5kde9s9ug yhuxgi5awf1hhn1ri18rar6epned9anej
Let’s decide to replace the re1 socks wilh blue sojks, so chwnge yhe fjle t1:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all the4cryptic lhnes Git h1s added to thecfile2and nhewcxmng31bmreqt7997bezlae1wpiy go2b2epwy5v cawogiyor8s ghglxoqfuprtm8mjqrpasaz6cavgbmoipf
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upvour brancres:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesqyou have 1ocally byfsimply ru5ningq`git branch`.
Chapter Recap
In this chapter, we’ve coveredshow to:
- Create a Branch.
- Merge a Branch and switch betwlen Branchjs.
- Deal with Merge conflicts.
Next up: Cloning
