Git: Guide to Solving Merge Conflicts - Stack Abuse
文章推薦指數: 80 %
Solve Merge Conflict with git merge --continue SALogotypeArticlesLearnWritewithUsSigninSignupPythonJavaScriptJavaHomeArticlesGit:GuidetoSolvingMergeConflictsDavidLandupMergeConflictsarisewhenmultipleagentsmodifythesamepartofafileandpushtheirchangestoaremotebranch.Whenyouattempttomerge,pullfromorpushtothesebranches-there'saconflict,andGitisn'tsurewhichsetofchangestoacceptandwhichtoreject,sincethere'snoobjectivemeasureofwhichchangeisright. MergeConflictsonlyarisewhenit'simpossibletodiscernupfrontwhichchangestokeep,andinthiscase,youhavetostepinandmakeadecision. TherearethreewaysyoucandealwithaMergeConflict-youcancontinuewiththemerge,byupdatingyourlocalfiletomatchwhatalreadyexistsinaremoterepository,youcanabortamerge,whichistypicallydoneifthere'samajorconflictthatisn'teasilyremediedoryoucankeepthelocalchangesfromtheworkingdirectoryandforcethemupontheremoterepository. Inthisguide,we'lltakealookatthethreewaysyoucanresolveaMergeConflictwithGit. HowdoMergeConflictsHappen? Let'squicklycreatearepositoryandamergeconflictsowecanobservewhichchangescauseditandhowthefileslooklikewhenweresolveit.We'llemulatearemote-workenvironmentbycreatingtwofoldersandtwoGitrepositorieswithinthem: $cdJane $gitinit $gitremoteaddoriginhttps://github.com/DavidLandup0/solving-merge-conflicts.git $cd.. $cdJohn $gitinit $gitremoteaddoriginhttps://github.com/DavidLandup0/solving-merge-conflicts.git JaneandJohnareworkingonaprojecttogether,andsharethesamefile-README.md.Johnwrotethefile,accidentallyleavinginatypo,andpushedittotheremoteorigin.Janecaughtthis,fixedthetypoandpushedthefiletotheremoteoriginagain. OnceJohnwantedtoaddanewlinetothefile,andmergehisbranchtothemainbranch-hisREADME.md(whichhasthetypo)wasinconflictwiththemain'sREADME.md,whichhadthetypofixed. OnJohn'sfeaturebranch,headdedaREADME.mdfile: $gitbranchfeature_john $gitcheckoutfeature_john Switchedtobranch'feature_john' $echo'WelcometoourREADMW.md!'>>README.md $gitaddREADME.md $gitcommit-m"AddedREADME.md" [feature_johnc44d65f]AddedREADME.md 1filechanged,1insertion(+) createmode100644README.md $gitpushoriginfeature_john gitpushoriginfeature_john Enumeratingobjects:4,done. Countingobjects:100%(4/4),done. ... Tohttps://github.com/DavidLandup0/solving-merge-conflicts.git *[newbranch]feature_john->feature_john $gitcheckoutmain Switchedtobranch'main' $gitmergefeature_john Updating48f09c2..c44d65f Fast-forward README.md|1+ 1filechanged,1insertion(+) createmode100644README.md $gitpushoriginmain Total0(delta0),reused0(delta0),pack-reused0 Tohttps://github.com/DavidLandup0/solving-merge-conflicts.git 48f09c2..c44d65fmain->main Johnaddedanewfiletohisbranchandpushedittohisremotebranchandthenmergedintomain-noissues,thereweren'tanyfilestherebeforethat. Now,Janewantstogetuptodatewiththemainbranchbypullingthechangesmadethere,noticesthetypo-fixesit,andpushesbacktomaintopreventothersfrompullingtheerroneouspiece: $cdJane $gitpulloriginmain remote:Enumeratingobjects:4,done. remote:Countingobjects:100%(4/4),done. ... Updating48f09c2..c44d65f Fast-forward README.md|1+ 1filechanged,1insertion(+) createmode100644README.md $gitbranchfeature_jane $gitcheckoutfeature_jane $echo'WelcometoourREADME.md!'>README.md $gitaddREADME.md $gitcommit-m"FixedtypoinREADME.mdfile" [feature_jane60f64fc]FixedtypoinREADME.mdfile 1filechanged,1insertion(+),1deletion(-) $gitpushoriginfeature_jane Enumeratingobjects:5,done. Countingobjects:100%(5/5),done. Writingobjects:100%(3/3),292bytes|292.00KiB/s,done. ... Tohttps://github.com/DavidLandup0/solving-merge-conflicts.git *[newbranch]feature_jane->feature_jane $gitcheckoutmain Switchedtobranch'main' $gitmergefeature_jane Updatingc44d65f..60f64fc Fast-forward README.md|2+- 1filechanged,1insertion(+),1deletion(-) $gitpushoriginmain Total0(delta0),reused0(delta0),pack-reused0 Tohttps://github.com/DavidLandup0/solving-merge-conflicts.git c44d65f..60f64fcmain->main Nowmainisclean-notypointheREADME.md.Though,John'sfileisnowoutofsync. Ifhetriespullingtheorigin'smain-aMergeConflictwilloccur. Ifhetriestopushachangewithaconflictingchangeontheremotebranch-aMergeConflictwilloccur. Ifhetriestorun$gitmergeontwobranchesthathaveconflictingchanges-aMergeConflictwilloccur. SayJohnaddedanewlinetothefile,pulledfromthemainbranchandthentriedmerginghisnewadditionintomainbeforepushinghis: $echo'Newline!'>>README.md $gitaddREADME.md $gitcommit-m"AddednewlinetoREADME.md" [feature_johnba27684]AddednewlinetoREADME.md 1filechanged,1insertion(+) $gitcheckoutmain Switchedtobranch'main' $gitpulloriginmain Fromhttps://github.com/DavidLandup0/solving-merge-conflicts *branchmain->FETCH_HEAD Updatingc44d65f..60f64fc Fast-forward README.md|2+- 1filechanged,1insertion(+),1deletion(-) $gitmergefeature_john Auto-mergingREADME.md CONFLICT(content):MergeconflictinREADME.md Automaticmergefailed;fixconflictsandthencommittheresult. Thereitis-MergeconflictinREADME.md.TherearethreethingsJohncandotosolvethisconflict,asheisnowintheMERGINGphase.WhenGitencountersaconflict,itdoesn'tabandonthemerge-itallowsyoutoattemptfixingtheissueonthespotorabandonitifyou'dliketo. FindMergeConflictSource Thefirststepyouneedtotakeisfindoutwhythere'saMergeConflictinthefirstplace.WhenintheMERGINGphase,asweare,Gitwillannotatethefilethat'scausingtheconflict.IfweopentheREADME.mdfileonJohn'slocalmachine,we'llsee: <<<<<<
>>>>>>feature_john The<<<<<<延伸文章資訊
- 1Git - git-merge Documentation
- 2git-merge Documentation - Git
Use git commit or git merge --continue to seal the deal. The latter command checks whether there ...
- 3How to Resolve Merge Conflicts in Git? | Simplilearn [Updated]
Learn the basics of Git merge conflicts and the advanced operations of Git, resolving a Git merge...
- 4Git指令git rebase - iT 邦幫忙
如果用之前提過的merge方式去合併(在master上用指令git merge iss2) ... git rebase --continue Applying: start fix iss2 ...
- 5git merge (Branching and Merging) - Git 中文开发手册 - 腾讯云
警告: git merge 不鼓励使用不平凡的未提交更改:尽管可能,但可能会让您处于难以在冲突情况下退出的状态。 第四种语法(“ git merge --continue ...