How to Resolve Merge Conflicts in Git? | Simplilearn [Updated]
文章推薦指數: 80 %
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.
延伸文章資訊
- 1Resolving a merge conflict using the command line
Competing line change merge conflicts · Open TerminalTerminalGit Bash. · Navigate into the local ...
- 2How to Resolve Merge Conflicts in Git? | Simplilearn [Updated]
A merge conflict is an event that takes place when Git is unable to automatically resolve differe...
- 3本地分支衝突- git - W3HexSchool - 六角學院
git merge dev Auto-merging all.css CONFLICT (content): Merge conflict in all.css Automatic merge ...
- 4合併發生衝突了,怎麼辦? - 為你自己學Git | 高見龍
git status On branch cat You have unmerged paths. (fix conflicts and run "git commit") (use "git ...
- 5Git 版本控制系統- 分支合併衝突與解決辦法 - Roya's Blog
這一次Push 就成功了,之前我們有說過 git pull 主要會將 git fetch 的內容直接執行 git merge ,這也才導致直接跳出合併的訊息視窗,這樣子看起來是不是 ...