Resolving a merge conflict using the command line
文章推薦指數: 80 %
Competing line change merge conflicts · Open TerminalTerminalGit Bash. · Navigate into the local Git repository that has the merge conflict. · Generate a list of ... SkiptomaincontentGitHubDocsAllproductsPullrequestsCommitchangestoyourprojectCreate&editcommitsAboutcommitsWithmultipleauthorsOnbehalfofanorganizationChangingacommitmessageView&comparecommitsComparingcommitsCommitviewsTroubleshootingcommitsCommitmissinginlocalcloneLinkedtowronguserCollaboratewithpullrequestsGettingstartedCollaborativedevelopmentWorkingwithforksAboutforksConfigurearemoteSyncingaforkAllowchangestoabranchDeletedorchangesvisibilityCodequalityfeaturesAboutstatuschecksProposechangesAboutbranchesCreate&deletebranchesAboutpullrequestsComparebranchesCreatingapullrequestCreateaPRfromaforkUsingqueryparameterstocreateapullrequestChangethestateRequestaPRreviewUpdatetheheadbranchChangethebasebranchCommittoPRbranchfromforkAddressmergeconflictsAboutmergeconflictsResolvemergeconflictsResolvemergeconflictsinGitReviewchangesAboutPRreviewsReviewproposedchangesFilterfilesMethods&functionsCommentonaPRViewaPRreviewReviewdependencychangesIncorporatefeedbackRequiredreviewsDismissaPRreviewCheckoutaPRlocallyIncorporatechangesAboutpullrequestmergesMergingapullrequestMergePRautomaticallyMergePRwithmergequeueClosingapullrequestRevertingapullrequestResolvingamergeconflictusingthecommandlineInthisarticleCompetinglinechangemergeconflictsRemovedfilemergeconflictsFurtherreadingYoucanresolvemergeconflictsusingthecommandlineandatexteditor.MacWindowsLinuxMergeconflictsoccurwhencompetingchangesaremadetothesamelineofafile,orwhenonepersoneditsafileandanotherpersondeletesthesamefile.Formoreinformation,see"Aboutmergeconflicts." Tip:YoucanusetheconflicteditoronGitHubtoresolvecompetinglinechangemergeconflictsbetweenbranchesthatarepartofapullrequest.Formoreinformation,see"ResolvingamergeconflictonGitHub." Competinglinechangemergeconflicts Toresolveamergeconflictcausedbycompetinglinechanges,youmustchoosewhichchangestoincorporatefromthedifferentbranchesinanewcommit. Forexample,ifyouandanotherpersonbotheditedthefilestyleguide.mdonthesamelinesindifferentbranchesofthesameGitrepository,you'llgetamergeconflicterrorwhenyoutrytomergethesebranches.Youmustresolvethismergeconflictwithanewcommitbeforeyoucanmergethesebranches. OpenTerminalTerminalGitBash. NavigateintothelocalGitrepositorythathasthemergeconflict. cdREPOSITORY-NAME Generatealistofthefilesaffectedbythemergeconflict.Inthisexample,thefilestyleguide.mdhasamergeconflict. $gitstatus >#Onbranchbranch-b >#Youhaveunmergedpaths. >#(fixconflictsandrun"gitcommit") ># >#Unmergedpaths: >#(use"gitadd..."tomarkresolution) ># >#bothmodified:styleguide.md ># >nochangesaddedtocommit(use"gitadd"and/or"gitcommit-a") Openyourfavoritetexteditor,suchasAtom,andnavigatetothefilethathasmergeconflicts. Toseethebeginningofthemergeconflictinyourfile,searchthefilefortheconflictmarker<<<<<<<.whenyouopenthefileinyourtexteditor>>>>>>>BRANCH-NAME.Inthisexample,onepersonwrote"openanissue"inthebaseorHEADbranchandanotherpersonwrote"askyourquestioninIRC"inthecomparebranchorbranch-a. Ifyouhavequestions,please <<<<<<>>>>>>branch-a Decideifyouwanttokeeponlyyourbranch'schanges,keeponlytheotherbranch'schanges,ormakeabrandnewchange,whichmayincorporatechangesfrombothbranches.Deletetheconflictmarkers<<<<<<>>>>>>andmakethechangesyouwantinthefinalmerge.Inthisexample,bothchangesareincorporatedintothefinalmerge: Ifyouhavequestions,pleaseopenanissueoraskinourIRCchannelifit'smoreurgent. Addorstageyourchanges. $gitadd. Commityourchangeswithacomment. $gitcommit-m"Resolvedmergeconflictbyincorporatingbothsuggestions." YoucannowmergethebranchesonthecommandlineorpushyourchangestoyourremoterepositoryonGitHubandmergeyourchangesinapullrequest. Removedfilemergeconflicts Toresolveamergeconflictcausedbycompetingchangestoafile,whereapersondeletesafileinonebranchandanotherpersoneditsthesamefile,youmustchoosewhethertodeleteorkeeptheremovedfileinanewcommit. Forexample,ifyoueditedafile,suchasREADME.md,andanotherpersonremovedthesamefileinanotherbranchinthesameGitrepository,you'llgetamergeconflicterrorwhenyoutrytomergethesebranches.Youmustresolvethismergeconflictwithanewcommitbeforeyoucanmergethesebranches. OpenTerminalTerminalGitBash. NavigateintothelocalGitrepositorythathasthemergeconflict. cdREPOSITORY-NAME Generatealistofthefilesaffectedbythemergeconflict.Inthisexample,thefileREADME.mdhasamergeconflict. $gitstatus >#Onbranchmain >#Yourbranchand'origin/main'havediverged, >#andhave1and2differentcommitseach,respectively. >#(use"gitpull"tomergetheremotebranchintoyours) >#Youhaveunmergedpaths. >#(fixconflictsandrun"gitcommit") ># >#Unmergedpaths: >#(use"gitadd/rm..."asappropriatetomarkresolution) ># ># deletedbyus:README.md ># >#nochangesaddedtocommit(use"gitadd"and/or"gitcommit-a") Openyourfavoritetexteditor,suchasAtom,andnavigatetothefilethathasmergeconflicts. Decideifyouwantkeeptheremovedfile.Youmaywanttoviewthelatestchangesmadetotheremovedfileinyourtexteditor. Toaddtheremovedfilebacktoyourrepository: $gitaddREADME.md Toremovethisfilefromyourrepository: $gitrmREADME.md >README.md:needsmerge >rm'README.md' Commityourchangeswithacomment. $gitcommit-m"ResolvedmergeconflictbykeepingREADME.mdfile." >[branch-d6f89e49]Mergebranch'branch-c'intobranch-d YoucannowmergethebranchesonthecommandlineorpushyourchangestoyourremoterepositoryonGitHubandmergeyourchangesinapullrequest. Furtherreading "Aboutmergeconflicts" "Checkingoutpullrequestslocally"
延伸文章資訊
- 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->...
- 3Git 版本控制系統- 分支合併衝突與解決辦法 - Roya's Blog
這一次Push 就成功了,之前我們有說過 git pull 主要會將 git fetch 的內容直接執行 git merge ,這也才導致直接跳出合併的訊息視窗,這樣子看起來是不是 ...
- 4git遇到衝突了怎麼辦?別緊張,解衝後就好了。 - iT 邦幫忙
When we use git merge to merge two branches, git has its own way to determine whether there is a ...
- 5本地分支衝突- git - W3HexSchool - 六角學院
git merge dev Auto-merging all.css CONFLICT (content): Merge conflict in all.css Automatic merge ...