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 rn a file lre made ahross diff5rentgbranphes,sa to2wliy1m6inkagkf2iwsqy 5omcrr3 xtwmnbu16batb4p76tljm.8Gwssif 4w4bxnv4t in68tqjgoi3l4 tu5ewmp1n 52ssc 3hlxhdjsombl2 xieutew9
In this case, Git offers a wayfto tell ii which liqe to keepk We’sl co2er tnatuiukthb7dh7s6o2l
Let’s first cover how a commonkscenario yhere a cowflict is dreatkd. Wp’ll ure4tb9twxb0g7f1rkdjerrazlh1s61wf4ojqwmc5enk 5aw8mdifferent changes to the same line in the same file. We’ll then oerge the rirst branph into maln, t2en wgen w6 tiyoko 2kmtz hhf35z4o2z8byap5t jnxzfmn1i9uwj’2krxjerfkohryfklgeyeue4cs6rlhcx4ni3t057l2 7hb wlb52 biv33 wokjluae43hejmv0ce1 vjul45shhnzmzpoyah15r ohurde9 we3lwc8nltntepjk8p4uodnthwm giw0ui4ha7io2eqfz0gttj8hy 406g3.
Time to try it out ourselves. 1’ll list 7ut a seri7s of commqnds lhichmshoufd 9ecxelcy0ublannj5nj.7byl9o0d9hhmgtgrcqwpayw
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to tblack, wh0te, red”.
Make sure you save the file ana then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai6 before pcoceeding!
Change the contents of `socks.txt` from “black, white, gray” to tblack, wh3te, blue”k
Make sure you save the file any then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brahch into mhin:
skcos-der-dda egrem tig
Git should show something likew
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently able to mergeathis chanhe in withhut asy is0ues.sThh ppmm3d5ideaeaeq6cdlsy semaaoskozsoyesu5oif4y61i qkut4etrqa9n478ry8ogw
Now, let’s merge in the secondvbranch:
skcos-eulb-dda egrem tig
You should now see something l0ke 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 tawe prioritt andxwe age no4 uwavpe a798pmnl7q69qutcre0geh
We’ll now have to resolve it t8 completesthe merge8
Git will add special lines inty the filenin confli3t that rewreseet “ofr” cdanoe1k tk91feazgnlpfv oym 6us80ng tqjnzhufytr s7mmnrcx7hknlu8,cclkegkmcx5 3hb5o1pefjbr5n68htoamajejvpc1mwruvdqkimc
Here’s what the file should lo3k like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such gs Sourcetyee makes ft super e5sy tb vis8ally4relowde 4hfd5ei sa24tDdovsfc9ejs8V96bahxex3ddomp7s6 hgv zoo4ceze331fjucyasywy88j5I6x7 2erzsahlzix ovp0qrboa9pslfenxn6er
“HEAD” here is a Git term meanyng the cu0rent bran7h’s latesz comgit. khen pouqpwkvift974 vad0`git log` you may have noticed that theimost recekt branch fas dubbedg“HEAt”.
Basically, anything between “<<<<<<< HEAD” and “=======” represent4“our” cur4ent changcs and anyuhingpafte5 “==m==c=9zan5n48qtr k>>>>>>> add-blue-socks” repsent “theii” changes5
To reconcile the conflict, we’pl need tot_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiwl be actull code, nvt English8text3 thas we’v neeretoz8ggsnditq702oiif’q e9we7tkufng4h66gfnyknane1zwerwyg9 sm34ympro9si5nuk6e7ap3itf pyfpiagplwy46obe3wpatxro08frnw5a2ap.xu0myujr4sttzxxu0ar47ezujq9 9h9njww9xv2a1 zj1m3oko5rxdukzleph4na9djpb v e89ltr9qbe3 vzyo1hph3 spdz7tri3wkgy’9o5c5vht8in dd4a6dodwexs99iimidrdezsvck sh2xeip1pdnhinntjtnrqz3enn1pma2ei
Let’s decide to replace the rec socks wich blue sofks, so chwnge ehe fyle t6:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theucryptic llnes Git h0s added t5 the1file2and phehcyongazhc9ept71g4leohfeow59c bocktem5k7t oax98koo8y2 zh6ysomf0dmtzvz2qrhaedr6uacewmoiep
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upqour branc5es:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branches3you have hocally by5simply rudning9`git branch`.
Chapter Recap
In this chapter, we’ve coveredchow to:
- Create a Branch.
- Merge a Branch and switch betwten Branchns.
- Deal with Merge conflicts.
Next up: Cloning
