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.
延伸文章資訊
- 1Git merge conflicts | Atlassian Git Tutorial
Git can handle most merges on its own with automatic merging features. A conflict arises when two...
- 2你一定會遇到衝突(git conflict) - Medium
使用git status 查看檔案的狀態,會發現MainActivity.kt目前變成Unmerged paths ... android studio內建解決衝突的工具,VCS ->Git->...
- 36.解決合併的衝突【教學1 使用分支】 | 連猴子都能懂的Git入門 ...
$ git merge issue3 Auto-merging myfile.txt CONFLICT (content): Merge conflict in myfile.txt Autom...
- 4git遇到衝突了怎麼辦?別緊張,解衝後就好了。 - iT 邦幫忙
When we use git merge to merge two branches, git has its own way to determine whether there is a ...
- 5How 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...