Git: Handling Merge Conflicts Using "Ours" and "Theirs"

文章推薦指數: 80 %
投票人數:10人

To resolve conflicts during a merge or rebase, we can use git checkout with one of two flags: --ours or --theirs. Subscribeh/git•16guidesTechnicallyspeaking,useofgitcheckout--ours/--theirsisonlyapplicableduringamerge.Youmightbewonderingaboutrebases,andI'llexplainthatinthenextstep. Forsimplicity,let'sstartwithabasicmergeconflict.Imagineourgithistorylookslikethis: A---B---Cfeature / D---E---F---Gmaster Theletterssignifyacommit,andeachcommitincludeschangestoourfile:myscript.py. Sochangesweremadetomyscript.pyinbothmasterandfeaturebranches.Acommonstrategyistoroutinelymergechangesfrommasterintoyourfeaturebranchduringdevelopmenttokeepthefeaturebranchfromgettingtoofaroutofdate.Butwhenwegotomergemasterintofeature,we'regoingtorunintotrouble: (feature)$gitmergemaster Auto-mergingmyscript.py CONFLICT(content):Mergeconflictinmyscript.py Automaticmergefailed;fixconflictsandthencommittheresult. Inmostcases,you'dwanttoopenmyscript.pyandsortoutthemergeconflicts.Butinsomecases,you'llwanttocompletelydisregardoneversionandkeeptheother.Thisiswheregitcheckout--ours/--theirscomesintoplay. Use--ourskeeptheversioninthecurrentbranch Sincewehaveourfeaturebranchcheckedout,wecanuse--ourstokeeptheversionofmyscript.pythatresidesinthefeaturebranch,anddisregardtheversionfrommaster. gitcheckout--oursmyscript.py Use--theirstokeeptheversionfromthebranchbeingmergedin And--theirsaccomplishestheopposite.Ifwewanttodiscardtheversionofmyscript.pythatresidesinourcurrentbranchandkeeptheversionfrommaster,wecanuse--theirs. gitcheckout--theirsmyscript.pyWhenwerunintomergeconflictsduringarebase,weareeffectivelyinthemiddleofamerge,sotherulesforgitcheckout--ours/--theirsfromabovestillapply.But,thetrickypartisidentifyingthe"current"branch.Letmeexplainwhathappensduringarebase. Whathappensduringarebase? Again,let'sassumethefollowinghistory: A---B---Cfeature / D---E---F---Gmaster Whenwerebasemaster"into"feature,whatwearereallydoingisthis: "Rollback"tothecommonancestorandsavethediff Inourcase,werollbacktocommitE,andsavethediffofeachcommitintroducebythefeaturebranch. A---B---C(savedintemporaryfiles) feature / D---E---F---Gmaster Resetthefeaturebranchtothecurrentcommitfrommaster Thefeaturebranchnowhasthesamehistoryasmaster. A---B---C(savedintemporaryfiles) feature / D---E---F---Gmaster Applythesavedchangesfromthefeaturebranch Noweachchangefromthefeaturebranch(A,B,andC)willbeappliedtothenewfeaturebranchonceagain.It'simportanttonote,forthesakeofthisguide,thatthisisaccomplishedthroughamerge. Thenewhistorylookslikethis: A---B---Cfeature / D---E---F---Gmaster Ok,sohowdoIusegitcheckout--ours/--theirsduringarebase? Thepointofthatlongwindedexplanationwastoshowthatwhenyouarefixingmergeconflictsinthemiddleofarebase,your"current"branchisnotlongeryouroriginalfeaturebranchbutratheranewbranchthatisuptodatewithmaster.Andthecommitsbeingmergeintothecurrentbrancharethecommitsfromyouroriginalfeaturebranch.So--oursand--theirswillappeartobeflippedaround. Use--ourstokeepchangesfromthebranchbeingrebasedonto(master) Atthebeginningoftherebase,wehadfeaturebranchcheckedout,soitmayseembackward,butwewilluse--ourstokeepchangesfrommaster. gitcheckout--oursmyscript.py Use--theirstokeepthechangesfromthe"current"branch(feature) Andnaturally,theoppositeistrue.Use--theirstokeepchangesfromthefeaturebranch. gitcheckout--theirsmyscript.pyIknowthiscanbeconfusing,sopleaseletmeknowinthecommentsbelowifyouthoughtthisguidewashelpfulorifyoucouldusefurtherclarification!NEXTUPJohn(304)May27,20212minutesSometimeswestartwritingcodewithoutmanagingourbranchesinadvance.Thenwemakecommitsinmaster(orsomeotherbasebranch)whenweintendedtocarveoffanewfeaturebranch.Continuereading



請為這篇文章評分?