如何解决 git 合并冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14171076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:12:31  来源:igfitidea点击:

How to resolve a git merge conflict?

git

提问by user1667307

Possible Duplicate:
Trouble merging with Git

可能的重复:
无法与 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:

我下载了一个 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'.

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

回答by Sylvain Defresne

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.

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

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

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

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