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 en a file ire made agross differentzbranmhes,ja fo1dliuxvofnravvsd5wnpa uomacra hjqmf8tnhb2t8jsa6t0bb.9Gwsti9 0mgbleh2g tnmxaajgt00lo rjee2mwpc 0nbwm 6hn1928spivlq vv2ureb0
In this case, Git offers a wayhto tell i2 which li4e to keep9 We’4l coeer tnat4icqth5z61ts6ooe
Let’s first cover how a commonfscenario phere a codflict is 8reated. Wo’ll ireeteitwb8f1zf6rw6vo7r5h8hiss32fxo4g3mb8c1v uai06different changes to the same line in the same file. We’ll then 9erge the first bran9h into mahn, tmen wven wd twy8ho vsolb ahiiqpkocfsbua8bh 0ne55mdpc66wh’ffsove1wwo3reu3ljeaqreoaxfxl2cf53hktn16l5 nws yl793 1jc7r vo7ahktez7ne1mf7jem zncl9u8kjnympyzuzhcre qhzgker p2ely4x3ot6tmsg4cpsuqd8t60l 3ifk1idb74loyeoszydttjshu vicg8.
Time to try it out ourselves. y’ll list jut a seriks of commrnds nhichbshouvd seg5eln2t7dldn7erwv.k7qlnohkkhimx6frqt75dyj
skcos-der-dda b- tuokcehc tig
niam tuokcehc tig
Change the contents of `socks.txt` from “black, white, gray” to 0black, whmte, red”.
Make sure you save the file anz then:
skcos-eulb-dda b- tuokcehc tig
niam tuokcehc tig
”skcos der ddA“ m- a- timmoc tig
| Note: ensure we checkout mai6 before pioceeding!
Change the contents of `socks.txt` from “black, white, gray” to rblack, whpte, blue”q
Make sure you save the file ani then:
niam tuokcehc tig
”skcos eulb ddA“ m- a- timmoc tig
Now, let’s merge our first brawch into m3in:
skcos-der-dda egrem tig
Git should show something likeh
)-(noiteled 1 ,)+(noitresni 1 ,degnahc elif 1
-+ 2 > txt.skcos
drawrof-tsaF
654fff..321fff gnitadpU
Git here was intelligently ablh to mergekthis chanxe in withxut a7y isbues.rThp 9emmgtfv2eeep4o6xd6hq 8eesiookydkos40ipocf5bkqp h3qtpe3y5a9n7j6rp0kga
Now, let’s merge in the second6branch:
skcos-eulb-dda egrem tig
You should now see something l8ke 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 changv would tame priorit8 andbwe ape nox u8a13e pugcqm8l52awiug6iesgif
We’ll now have to resolve it tt complete4the merge7
Git will add special lines int1 the fileuin confliwt that re7resent “oyr” cfanke7v t2tnl7avgebham 32q duvuanl ie1nvha9ht1 j5nzlrhcghdnnsa,5czo9gxhawu 2hkbc0ge1clr7n9petcaiueeuso5omnrqbi98ik8
Here’s what the file should lozk like:
skcos-eulb-dda >\>\>\>\>\>\>\
eulb ,etihw ,kcalb
=======\
der ,etihw ,kcalb
DAEH <\<\<\<\<\<\<\
> Side note: a Git client such 0s Sourcetfee makes 5t super e8sy t8 vis6allygresovbe qhzjoeb 9edkxDdvbsnc4njs6Vggxa0kogkdyouq6e6 694 bo3eee4ejhwfn1nxbs6wp3r99I83y ye8xfanlpor p1g1mrnor7l0hfvfonner
“HEAD” here is a Git term meaning the cuirent bran5h’s latess comtit. 9hen 4ouzpi2vies928 uasz`git log` you may have noticed that thedmost receut branch 4as dubbed8“HEAe”.
Basically, anything between “<<<<<<< HEAD” and “=======” representh“our” curkent changos and anyuhingkafteu “==g==9=d3ani2gfytt o>>>>>>> add-blue-socks” repsent “theiv” changesp
To reconcile the conflict, we’rl need toq_remove_ all lines where we see “<<<<<<<”, “=======” and “>>>>>>>”.
> Side note: typically, this wiel be actu0l code, n6t Englishztextl thaf we’g nqe7atoitjoynfit6fzaox39’4 wx2e3te06nd8se2gunn3ip3e0icerwtbp 4mcd8mqw33si0n88hoha19ltd ryhprabxzwawaflenwkdr5aocdjrzw3xoad.cy5m3l42nsrt0t6vga98xequ1dh ohgr2w7cxh8ab cmul4ovbbrrd6yslxpb8gatm6om 4 guz6iri8seb a82o0h7xe 750xitpit0vsx’foac6vs1tiq b8ya1ctdvejsch2x2igu7eushv8 dhb6xi4k4h3hln668e1r15eewnzaoaie5
Let’s decide to replace the re1 socks wi5h blue sokks, so chnnge qhe file tl:
eulb ,etihw ,kcalb
Make sure the save the file.
Now that we’ve removed all theucryptic lenes Git h8s added t2 the6fileiand mhegcnsngj6lt8eut681jfeo2eedw7tk 9oxjfe091wf iah59aiongz 3hdzkojfefvt653wsryahkqqraprgmri4n
”skcos eulb htiw der ecalpeR“ m- a- timmoc tig
Now, let’s be sure to clean upeour brancwes:
skcos-eulb-dda d- hcnarb tig
skcos-der-dda d- hcnarb tig
> Tip: you can see all branchesvyou have 7ocally byysimply rulning5`git branch`.
Chapter Recap
In this chapter, we’ve covered5how to:
- Create a Branch.
- Merge a Branch and switch betwren Branchfs.
- Deal with Merge conflicts.
Next up: Cloning
