How to Resolve Merge Conflicts in Git? | Simplilearn [Updated]

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

A merge conflict is an event that takes place when Git is unable to automatically resolve differences in code between two commits. Git can merge ... DevOpsDataScience&BusinessAnalyticsAI&MachineLearningProjectManagementCyberSecurityCloudComputingDevOpsBusinessandLeadershipQualityManagementSoftwareDevelopmentAgileandScrumITServiceandArchitectureDigitalMarketingBigDataCareerFast-trackEnterpriseOtherSegmentsVideoTutorialsArticlesEbooksFreePracticeTestsOn-demandWebinarsHomeResourcesDevOpsGitTutorialHowtoResolveMergeConflictsinGit?TutorialPlaylistGitTutorialOverviewGITTutorialForBeginnerLesson-1WhatisGit:Features,CommandandWorkflowinGitLesson-2GitInstallationonWindows:A(Step-by-Step)GuideLesson-3AllTheGitCommandsYouNeedtoKnowAboutLesson-4GitPushCommandExplainedWithDemoLesson-5GitPullRequestBasicsExplainedwithDemoLesson-6HowtoResolveMergeConflictsinGit?Lesson-7WhatisGitHubAndHowToUseIt?Lesson-8GitvsGitHub:WhataretheMajorDifferences?Lesson-9WhatisGitLabandHowTouseIt?Lesson-10Top40GitInterviewQuestionsandAnswersLesson-11HowtoResolveMergeConflictsinGit?Lesson7of11BySimplilearnLastupdatedonAug5,2022630853PreviousNextTutorialPlaylistGitTutorialOverviewGITTutorialForBeginnerLesson-1WhatisGit:Features,CommandandWorkflowinGitLesson-2GitInstallationonWindows:A(Step-by-Step)GuideLesson-3AllTheGitCommandsYouNeedtoKnowAboutLesson-4GitPushCommandExplainedWithDemoLesson-5GitPullRequestBasicsExplainedwithDemoLesson-6HowtoResolveMergeConflictsinGit?Lesson-7WhatisGitHubAndHowToUseIt?Lesson-8GitvsGitHub:WhataretheMajorDifferences?Lesson-9WhatisGitLabandHowTouseIt?Lesson-10Top40GitInterviewQuestionsandAnswersLesson-11TableofContentsViewMore Gitisoneofthemostpopularsource-controlsystemsthatenablesoftwaredevelopmentprofessionalsinallindustries,enablingmultipleteammemberstoworkconcurrentlyonprojects.Sincemanyusersaresimultaneouslyworkingfromdifferentplacesonthesamefile,however,youmayendupwithamergeconflict.ThisarticleexplainsthebasicsofGitmergeconflictsandoneoftheadvancedoperationsofGit:resolvingaGitmergeconflict.  First,we’llgooverthebasicsofGit. WhatisGit? Gitisanopen-source,distributedversioncontrolsystem(VCS),whichhasaremoterepositoryontheserver-sideandalocalrepositoryontheclient-side.Thismeansthatthefileorcodeisnotpresentinacentralserver,butthereisacopyofthefilestoredontheclient’scomputer.  DevOpsEngineerMaster'sProgramBridgebetweensoftwaredevelopersandoperationsExploreCourse Adistributedversioncontrolsystemenablesmultipledeveloperstoworkinparallelwitheachotherwithoutanycodeconflicts.Gitenablesdeveloperstorevertandgobacktoanolderversionofthecodewhenevernecessary.  Githelpsbothdevelopersandnon-techprofessionalsbykeepingtrackoftheirprojectfiles.Itmakesiteasierformultipleindividualstoworktogether,anditplaysanextremelysignificantroleinbigprojectsthatinvolvelargeteams. BasicGitCommands gitconfig  gitinit gitadd  gitdiff gitcommit gitreset gitstatus gitmerge gitpush gitpull WhatisaGitMergeConflict? AmergeconflictisaneventthattakesplacewhenGitisunabletoautomaticallyresolvedifferencesincodebetweentwocommits.Gitcanmergethechangesautomaticallyonlyifthecommitsareondifferentlinesorbranches. ThefollowingisanexampleofhowaGitmergeconflictworks: Let’sassumetherearetwodevelopers:DeveloperAandDeveloperB.Bothofthempullthesamecodefilefromtheremoterepositoryandtrytomakevariousamendmentsinthatfile.Aftermakingthechanges,DeveloperApushesthefilebacktotheremoterepositoryfromhislocalrepository.Now,whenDeveloperBtriestopushthatfileaftermakingthechangesfromhisend,heisunabletodoso,asthefilehasalreadybeenchangedintheremoterepository. Topreventsuchconflicts,developersworkinseparateisolatedbranches.TheGitmergecommandcombinesseparatebranchesandresolvesanyconflictingedits. NowthatwehavegonethroughthebasicsoftheGitmergeconflict,let’slookatthevarioustypesofconflictsnext. FREEGITTrainingLearnthebasicsofGITEnrollNow TypesofGitMergeConflicts  Therearetwopointswhenamergecanenteraconflictedstate: 1.StartingtheMergeProcess  Iftherearechangesintheworkingdirectory’sstageareaforthecurrentproject,mergingwon’tstart.  Inthiscase,conflictshappenduetopendingchangesthatneedtobestabilizedusingdifferentGitcommands. 2.DuringtheMergeProcess Thefailureduringthemergeprocessindicatesthatthereisaconflictbetweenthelocalbranchandthebranchbeingmerged. Inthiscase,Gitresolvesasmuchaspossible,buttherearethingsthathavetoberesolvedmanuallyintheconflictedfiles. WewillnowgooverresolvingmergeconflictsinGit. HowtoResolveMergeConflictsinGit? ThereareafewstepsthatcouldreducethestepsneededtoresolvemergeconflictsinGit. Theeasiestwaytoresolveaconflictedfileistoopenitandmakeanynecessarychanges Aftereditingthefile,wecanusethegitaddacommandtostagethenewmergedcontent Thefinalstepistocreateanewcommitwiththehelpofthegitcommitcommand Gitwillcreateanewmergecommittofinalizethemerge LetusnowlookintotheGitcommandsthatmayplayasignificantroleinresolvingconflicts. GitCommandstoResolveConflicts 1.gitlog--merge  Thegitlog--mergecommandhelpstoproducethelistofcommitsthatarecausingtheconflict 2.gitdiff  Thegitdiffcommandhelpstoidentifythedifferencesbetweenthestatesrepositoriesorfiles 3.gitcheckout  Thegitcheckoutcommandisusedtoundothechangesmadetothefile,orforchangingbranches 4.gitreset--mixed  Thegitreset--mixedcommandisusedtoundochangestotheworkingdirectoryandstagingarea 5.gitmerge--abort Thegitmerge--abortcommandhelpsinexitingthemergeprocessandreturningbacktothestatebeforethemergingbegan 6.gitreset Thegitresetcommandisusedatthetimeofmergeconflicttoresettheconflictedfilestotheiroriginalstate FREEDevOpsCertificationTrainingMasterthefundamentalsofDevOpsStartLearning Demo:ResolvingGitMergeConflicts First,initializetworepositories:  gitinitA gitinitB AddtheremoteaddressintheArepository: gitremoteaddorigin*address* Thenextstepistopullallthechangesinthecentralrepositorytothelocalrepository. gitpulloriginmaster FollowthesameprocesstoaddtheoriginintheBrepository. gitremoteaddorigin*address* Thepullcommandisexecutedagaintoretrieveallthecontentfromtheremoterepositoryandmoveittothelocalrepository. gitpulloriginmaster Bothoftheserepositoriesrepresenttwodifferentrepositoriesoftwodifferentdevelopers. PostGraduatePrograminDevOpsDesignedincollaborationwithCaltechCTMEExploreCourse Let'sgetbacktotheArepository. cd../A IntheArepository,areadmefileisopenedinordertomakevariouschanges. viREADME.md Makethenecessarychangesinthefile,andthensaveit. Thegitstatuscommandisthenexecutedinordertoseethereflectedchanges.  gitstatus Thenextstepistoaddthesechangestothestagingareaandcommitthem. gitadd. gitcommit-m*commitmessage* FreeCourse:IntroductiontoDevOpsToolsMastertheFundamentalsofDevOpsToolsEnrollNow Afterthecommitisfinished,thechangedfileispushedtotheremoterepository. gitpushoriginmaster Now,returntotheBrepository.  cdB Openareadmefile  viREADME.md Makechangestothefile,saveit,andcloseit.Afterthat,addthechangedfileandcommitit. gitadd. gitcommit-m*commitmessage* Thenextstepistopushthefiletotheremoterepository. gitpush Anerrorisshown,meaningthattheupdatesarerejected. Next,weneedtoexecute: git--rebaseoriginmaster Currently,therearevisibleconflictsthatneedtoberesolvedmanually. Ifyouwanttoskipthiscommit,youcantypegitrebase--skip,orifyouwanttoabortthisrebase,youcantypegitrebase--abort. Aftermanagingthisconflictmanually,wewillopenthemergetool. gitmergetool Afterweinputthiscommand,allofthefileswillbeprocessed. Thesearealltheprocessesandthemodificationsdoneinthefile. Youcanseethreedifferentfilesthere,andyoucanseeeverythingthatwasaddedorremoved.  Afterscrolling,youcanverifywhereexactlytheconflicthappened. Youcanthendecideifyouwanttocontinuewiththisparticularfileornot.Iwillproceedwithremovingthatline. Manualmodificationshaveallowedustoresolvefileconflicts.Savethefileandclosethefinalfile. Next,wewillrun: gitrebase--continue Now,whentheconflictisresolved,wemustbeabletopushthefiletotheremoterepository. Youcannowcheckthecommitsinyourremoterepository. ExploretheopportunitiesofworkingwiththelatestDevOpstoolssuchasDocker,Git,Jenkins,andmorebychoosingourDevOpsEngineerCertificationCourse.GrabyourseatfastbycontactingouradmissioncounselorTODAY! Conclusion WehopethatthiscomprehensivetutorialwillhelpyouwithGitmergeconflicts.Wewentoverthebasicsofthemergeconflicts,includingthetypesofmergeconflictsandpossibleexplanationsfortheiroccurrence.Wealsoprovidedadetailedexamplethroughastep-by-stepdemo.Inthedemo,wesawhowwecanmanuallyresolvemergeconflicts.  WanttoLearnMore? Aswementioned,Gitisapowerfulandpowerfultoolusedbycompaniesacrosstheworld.Thatmeansthatqualifiedprofessionalsareinhighdemand.Ifyouwanttokickstartyourcareer,checkoutSimplilearn'sDevOpsCertificationCoursetoday.Init,youwillgainin-depthknowledgeofGitandotherpopularversioncontrolsystems,andmuchmore. FindourDevOpsEngineerOnlineBootcampintopcities:NameDatePlaceDevOpsEngineerClassstartson27thAug2022,WeekendbatchYourCityViewDetailsDevOpsEngineerClassstartson3rdSep2022,WeekendbatchSingaporeViewDetailsDevOpsEngineerClassstartson10thSep2022,WeekendbatchYourCityViewDetailsAbouttheAuthorSimplilearnSimplilearnisoneoftheworld’sleadingprovidersofonlinetrainingforDigitalMarketing,CloudComputing,ProjectManagement,DataScience,IT,SoftwareDevelopment,andmanyotheremergingtechnologies.ViewMoreRecommendedResourcesAllTheGitCommandsYouNeedtoKnowAboutVideoTutorialDevOpsInterviewGuideEbookGitRebasevs.Merge:ACompleteGuideArticleWhatisGit:Features,CommandandWorkflowinGitVideoTutorialTop40GitInterviewQuestionsandAnswersVideoTutorialDataScienceCareerGuide:AComprehensivePlaybookToBecomingADataScientistEbookprevNext DisclaimerPMP,PMI,PMBOK,CAPM,PgMP,PfMP,ACP,PBA,RMP,SP,andOPM3areregisteredmarksoftheProjectManagementInstitute,Inc.



請為這篇文章評分?