如何解决git合并冲突? [英] How to resolve a git merge conflict?

查看:1348
本文介绍了如何解决git合并冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

麻烦与Git合并


我下载了一个git repo做了一些修改(删除一些文件),然后提交它。然后我做了git checkout -b新分支。这里我没有删除这些文件,但是我做了一些修改并提交了它。现在我回到我的主分支,试图做一个混帐new_branch_i_created,但是说:

 致命︰'merge'是不可能,因为你有没有合并的文件。 
请修正它们在工作树中,然后使用'git add / rm< file>'作为
来标记分辨率并进行提交,或使用'git commit -a'。

我做错了什么?

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

  GDCatalog / .classpath:需要合并
GDCatalog / src / com / cyrilmottier / android / gdcatalog / CatalogActivity.java:需要合并
GDCatalog / src / com / cyrilmottier / android / gdcatalog / PagedViewActivity.java:需要合并
GreenDroid-GoogleAPIs / .classpath:需要合并
GreenDroid / .classpath:需要合并
GreenDroid / src / greendroid / widget / PagedView.java:需要合并
错误:您需要首先解析当前索引


解决方案

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



要解决此问题,您需要编辑冲突的文件。对于每个文件,您必须解决合并。这些冲突将被<<<<<<<<<< c $ c> c>,<< ========= >>>>>>>>>>< / code>标记。如何解决它们将取决于所做的更改(您可能希望保留所做的更改,远程分支中的更改或两者的组合)。



已解决所有冲突,那么您必须为每个文件执行 git add 。然后 git commit 。预填充的提交消息将指示这是合并提交,并且解决了一些冲突。你可能不需要编辑它,除非你的编码风格要求你的提交信息有特定的格式。所以,总结一下,你必须这样做: / p>

  $ emacs GDCatalog / .classpath 
...解决冲突...
$ git add GDCatalog / .classpath
...对所有其他文件执行相同操作...
$ git commit


Possible Duplicate:
Trouble merging with Git

I downloaded a git repo made some changes(deleted some files) and then commited it. Then I did git checkout -b "new branch". Here I didnt delete the files but I did make some changes and commited it. Now I went back to my master branch and tried to do a git merge new_branch_i_created but that said:

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'.

What am I doing wrong?

And if I try to checkout to the new branch from my master branch it says:

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

解决方案

The error message from git say it all. You have un-merged changes between your two branches that need to be resolved before submitted the merge commit. Those merge conflicts happen because the file have been modified in the two branches.

To resolve this, you need to edit the files in conflict. For each file, you'll have to resolve the merge. The conflicts will be surrounded by <<<<<<<<<<<, =========== and >>>>>>>>>> markers. How you resolve them will depends on the changes (you may want to keep your changes, the changes in the remote branch or a combination of the two).

Once you've resolved all the conflicts, then you'll have to do git add for each file. And then git commit. The pre-filled commit message will indicate this is a merge commit and that there were some conflict resolved. You probably don't have to edit it unless your coding style mandate some particular formatting of your commit message.

So, to summarise, you'll have to do:

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

这篇关于如何解决git合并冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆