Git: Handling Merge Conflicts Using "Ours" and "Theirs"
文章推薦指數: 80 %
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
延伸文章資訊
- 1Advanced Merging - Git SCM
If you pass it diff3 , Git will use a slightly different version of conflict markers, not only gi...
- 2實戰版本衝突(Dealing with Conflict) - Practical guide for git users
git checkout --theirs <conflict file>. 上述的指令顧名思義就是把有衝突的檔案還原到對方的版本。 反之,如果要以我們自己的版本為準時,就可以改用--ours :.
- 3Quickest Way to Resolve Most Merge Conflicts - Git
Find files with merge conflict ... Change working directory to project folder. ... Search for all...
- 4Fixing Git Merge Conflicts: Reference and Examples
Fix all conflicts using "their" changes
- 5Use Theirs With Git Merge | Delft Stack
Use --strategy-option to Resolve Conflicts in Git