【问题标题】:How to resolve a git merge conflict? [duplicate]如何解决 git 合并冲突? [复制]
【发布时间】:2013-01-05 10:57:19
【问题描述】:

可能重复:
Trouble merging with Git

我下载了一个 git repo 做了一些更改(删除了一些文件),然后提交了它。然后我做了 git checkout -b "new branch"。在这里我没有删除文件,但我做了一些更改并提交了它。现在我回到我的主分支并尝试做一个 git merge new_branch_i_created 但那说:

fatal: 'merge' is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>' as
appropriate to mark resolution and make a commit, or use 'git commit -a'.

我做错了什么?

如果我尝试从我的主分支结帐到新分支,它会说:

GDCatalog/.classpath: needs merge
GDCatalog/src/com/cyrilmottier/android/gdcatalog/CatalogActivity.java: needs merge
GDCatalog/src/com/cyrilmottier/android/gdcatalog/PagedViewActivity.java: needs merge
GreenDroid-GoogleAPIs/.classpath: needs merge
GreenDroid/.classpath: needs merge
GreenDroid/src/greendroid/widget/PagedView.java: needs merge
error: you need to resolve your current index first

【问题讨论】:

  • 如何解决这些错误?我运行什么命令将第二个分支与主分支合并
  • 一个问题:在尝试执行git merge 之前的最后一条命令是什么?问题是未提交的更改阻止了合并,还是合并期间发生了合并冲突(例如有.git/MERGE_HEAD)?

标签: git


【解决方案1】:

来自 git 的错误信息说明了一切。在提交合并提交之前,您的两个分支之间有未合并的更改需要解决。发生这些合并冲突是因为文件已在两个分支中进行了修改。

要解决此问题,您需要编辑有冲突的文件。对于每个文件,您必须解决合并问题。冲突将被&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;===========&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; 标记包围。如何解决它们将取决于更改(您可能希望保留更改、远程分支中的更改或两者的组合)。

解决所有冲突后,您必须为每个文件执行git add。然后git commit。预先填写的提交消息将表明这是一个合并提交,并且已经解决了一些冲突。您可能不必编辑它,除非您的编码风格要求您的提交消息采用某种特定格式。

所以,总而言之,你必须这样做:

$ emacs GDCatalog/.classpath
... resolve conflict ...
$ git add GDCatalog/.classpath
... do the same for all other files ...
$ git commit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-15
    • 2018-04-19
    • 2018-08-04
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多