简体   繁体   English

Visual Studio Code 如何解决与 git 的合并冲突?

[英]Visual Studio Code how to resolve merge conflicts with git?

I tried to merge my branch with another branch and there was a merge conflict.我试图将我的分支与另一个分支合并,但出现了合并冲突。 In Visual Studio Code (version 1.2.1) I resolved all of the issues, however when I try to commit it keeps giving me this message:在 Visual Studio Code(版本 1.2.1)中,我解决了所有问题,但是当我尝试提交时,它一直给我这条消息:

You should first resolve the un-merged changes before committing your changes.在提交更改之前,您应该首先解决未合并的更改。

I've tried googling it but I can't find out why it won't let me commit my changes, all of the conflicts have disappeared.我试过用谷歌搜索它,但我找不到为什么它不让我提交我的更改,所有的冲突都消失了。

With VSCode you can find the merge conflicts easily with the following UI.使用 VSCode,您可以通过以下 UI 轻松找到合并冲突。 在此处输入图像描述

(if you do not have the topbar, set "editor.codeLens": true in User Preferences) (如果没有"editor.codeLens": true在用户首选项中设置"editor.codeLens": true

It indicates the current change that you have and incoming change from the server.它指示您拥有的当前更改和来自服务器的传入更改。 This makes it easy to resolve the conflicts - just press the buttons above <<<< HEAD .这使得解决冲突变得容易 - 只需按下<<<< HEAD上方的按钮。

If you have multiple changes and want to apply all of them at once - open command palette (View -> Command Palette) and start typing merge - multiple options will appear including Merge Conflict: Accept Incoming , etc.如果您有多项更改并希望一次应用所有更改 - 打开命令面板(视图 - > 命令面板)并开始输入 merge - 将出现多个选项,包括Merge Conflict: Accept Incoming等。

经过反复试验,我发现您需要暂存具有合并冲突的文件,然后您可以提交合并。

For those who are having a hard time finding the "merge buttons".对于那些很难找到“合并按钮”的人。

The little lightbulb icon with the merge options only shows up if you click precisely on the "merge conflict marker":仅当您精确单击“合并冲突标记”时,才会显示带有合并选项的小灯泡图标:

<<<<<<<

Steps (in VS Code 1.29.x):步骤(在 VS Code 1.29.x 中):

  1. Click "Source Control" button on left.单击左侧的“源代码管理”按钮。
  2. See MERGE CHANGES in sidebar.请参阅侧栏中的合并更改。
  3. Those files have merge conflicts.这些文件有合并冲突。

VS Code > 源代码管理 > 合并更改(示例)

The error message you are getting is a result of Git still thinking that you have not resolved the merge conflicts.您收到的错误消息是 Git 仍然认为您尚未解决合并冲突的结果。 In fact, you already have, but you need to tell Git that you have done this by adding the resolved files to the index.事实上,你已经有了,但你需要告诉 Git 你已经通过解析的文件添加到索引中来做到这一点。

This has the side effect that you could actually just add the files without resolving the conflicts, and Git would still think that you have.这有一个副作用,您实际上可以只添加文件而不解决冲突,Git 仍然会认为您有。 So you should be diligent in making sure that you have really resolved the conflicts.所以你应该努力确保你已经真正解决了这些冲突。 You could even run the build and test the code before you commit.您甚至可以在提交之前运行构建并测试代码。

For VS Code 1.38 or if you could not find any "lightbulb" button.对于 VS Code 1.38 或者如果您找不到任何“灯泡”按钮。 Pay close attention to the greyed out text above the conflicts;密切注意冲突上方的灰色文本; there is a list of actions you can take.您可以采取一系列行动。

For VSCode 1.70.2 in merge view from source control panel there are checkboxes on "yours" and "theirs" sides, next to the line numbers in the middle of selected conflict block , instead of text buttons above lines.对于源代码控制面板的合并视图中的 VSCode 1.70.2,“yours”和“theirs”两侧的复选框位于所选冲突块中间的行号旁边,而不是行上方的文本按钮。

If there are conflicts: Use vs-code to solve them file by file.如果有冲突:使用vs-code逐个文件解决。 Click button "complete merge" in vs-code after every file.在每个文件之后单击 vs-code 中的“完全合并”按钮。 When there are no files left, run command:当没有剩余文件时,运行命令:

git commit

( Don't rely on the vs-code "commit" button, it will be grayed out, which is wrong. ) (不要依赖 vs-code 的“提交”按钮,它会变灰,这是错误的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM