git 如何解决git中的冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47588838/
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-19 13:00:11  来源:igfitidea点击:

How to fix conflicts in git?

gitmergegit-mergegit-merge-conflict

提问by Roman

When I try to pull I have the following message:

当我尝试拉我有以下消息:

Automatic merge failed; fix conflicts and then commit the result.

So, I try to fix conflict (as git suggest) for that I follow theseinstructions.

因此,我尝试解决冲突(如 git 建议),因为我遵循这些说明。

Among the first things that I need to do is to open the "problematic" file in a text editor and find lines like that

我需要做的第一件事是在文本编辑器中打开“有问题的”文件并找到这样的行

If you have questions, please
<<<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>>>> branch-a

The problem is that I do not see things like that in my file. In particular I searched for HEAD and it is not there.

问题是我在我的文件中没有看到类似的东西。特别是我搜索了 HEAD 并且它不存在。

回答by danglingpointer

There are several ways to solve git conflicts, I will explain here using GUI and text editor way.

解决git冲突的方法有多种,这里我会用GUI和文本编辑器的方式来解释。

Solving git conflict using text editor.

使用文本编辑器解决 git 冲突。

Open any one of your favourite editor, example, Notepad, gedit, vim, nano or even Eclipse.

打开您最喜欢的任何一种编辑器,例如记事本、gedit、vim、nano 甚至 Eclipse。

  1. Whenever there is conflict in a file git add conflict marker that looks like this <<<<<<<<.

  2. When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <<<<<<< HEAD

  3. ========, it divides your changes from the other branch as >>>>>>>>YOUR_BRANCH_NAME

  4. You can decide if you want keep your branch changes or not. If you want to keep the changes what you did, delete the conflict marker they are,<<<<<<<, =======, >>>>>>>and then do a merge.

  1. 每当文件中存在冲突时,git 添加看起来像这样的冲突标记 <<<<<<<<

  2. 当您在文本编辑器中打开文件时,您将在该行之后看到来自 HEAD 或基本分支的更改 <<<<<<< HEAD

  3. ========,它将您的更改与其他分支划分为 >>>>>>>>YOUR_BRANCH_NAME

  4. 您可以决定是否要保留分支更改。如果您想保留所做的更改,请删除它们所在的冲突标记,<<<<<<<, =======, >>>>>>>然后进行合并。

Commands to commit the changes.

提交更改的命令。

git add . or git add "your_file"
git commit -m "Merge conflicts resolved"


Solving git conflict your GUI

解决 git 冲突你的 GUI

What if you have so many merge conflicts in files? In that case you can use GUI, well its just personal choice whether to opt for GUI or not. In GUI, you can see side by side diff. It is easy for the beginners to see the changes.

如果文件中有这么多合并冲突怎么办?在这种情况下,您可以使用 GUI,这只是个人选择是否选择 GUI。在 GUI 中,您可以看到并排差异。初学者很容易看到变化。

You need to configure your git to use the mergetool you want to use. Example: meld, kdiff3 or vimdiff. Again it's upto the user what they prefer. I use meld. Here you need to install these tool, after installation you have to configure it.

您需要配置您的 git 以使用您要使用的合并工具。示例:meld、kdiff3 或 vimdiff。再次取决于用户他们喜欢什么。我用融合。这里需要安装这些工具,安装后需要进行配置。

git config merge.tool meld 

you can solve merge conflicts

你可以解决合并冲突

git mergetool -t meld

Then you can follow the step 2 to 4 from solving using text editor, to solve the merge conflicts.

然后您可以按照使用文本编辑器解决的步骤2到4来解决合并冲突。