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<<<<<<延伸文章資訊
- 1合併發生衝突了,怎麼辦? - 為你自己學Git | 高見龍 - gitbook.tw
git status On branch cat You have unmerged paths. (fix conflicts and run "git commit") (use "git ...
- 2git-merge Documentation - Git
Use git commit or git merge --continue to seal the deal. The latter command checks whether there ...
- 3Git: Guide to Solving Merge Conflicts - Stack Abuse
Solve Merge Conflict with git merge --continue
- 4git遇到衝突了怎麼辦?別緊張,解衝後就好了。
我們只留下meat branch上的那行敘述,如上圖。 Enter: 輸入: git add fileOnlyExistingInMeatBranch git merge --continue ...
- 5Git - git-merge Documentation