git 如何解决EGit中的冲突

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

How to resolve conflicts in EGit

eclipsegitegit

提问by Sionnach733

I am using EGit on Eclipse v4.3(Kepler). I want to commit and push my changes. I do a pull first and one file is conflicting. After manually resolving the conflict (local and remote are the same now), I am still running into problems.

我在Eclipse v4.3(Kepler)上使用 EGit 。我想提交并推送我的更改。我先拉,一个文件发生冲突。手动解决冲突(本地和远程现在相同)后,我仍然遇到问题。

Here are the error messages for each action:

以下是每个操作的错误消息:

Push to upstream

推向上游

master: master [rejected - non-fast-forward]

master: master [拒绝 - 非快进]

Pull

Cannot pull into a repository with state: MERGING_RESOLVED

无法拉入状态为:MERGING_RESOLVED 的存储库

Mark as merged

标记为合并

Failed to add resource to index Failed to add resource to index Exception caught during execution of add command

无法将资源添加到索引 无法将资源添加到索引 执行添加命令期间捕获异常

Hard reset

硬重置

An internal error occurred during: "Resetting to refs/heads/master". Exception caught during execution of reset command. {0}

在以下过程中发生内部错误:“重置为 refs/heads/master”。在执行重置命令期间捕获异常。{0}

How can I remove the conflict and push my changes? What am I doing wrong?

如何消除冲突并推送我的更改?我究竟做错了什么?

回答by dan carter

Are you using the Team Synchronise view? If so that's the problem. Conflict resolution in the Team Synchronise view doesn't work with EGit. Instead you need to use the Git Repository view.

您是否在使用团队同步视图?如果是这样,那就是问题所在。团队同步视图中的冲突解决不适用于 EGit。相反,您需要使用 Git Repository 视图。

Open the Git perspective. In the Git Repository view, go to on BranchesLocalmasterand right click → Merge...

打开 Git 透视图。在 Git Repository 视图中,转到BranchesLocalmaster并右键单击 → Merge...

It should auto select Remote Trackingorigin/master. Press Merge.

它应该自动选择远程跟踪origin/master。按Merge

It should show result:conflict.

它应该显示result:conflict.

Open the conflicting files. They should have an old sk000l >>>> ===== <<<< style merge conflict in the files. Edit the file to resolve the conflict, and save.

打开冲突的文件。他们应该有一个旧的 sk000l >>>> ====== <<<< 文件中的样式合并冲突。编辑文件以解决冲突,然后保存。

Now in the 'Git Staging' view, it should show the changed file in 'Unstaged Changes'. Right click and 'Add to Index'

现在在“Git Staging”视图中,它应该在“Unstaged Changes”中显示更改的文件。右键单击并“添加到索引

Enter image description here

在此处输入图片说明

Repeat for any remaining files.

对任何剩余的文件重复此操作。

Now from the 'git staging' view, commit and push. As Git/Eclipse now knows that you have merged the remote origin changes into your master, you should avoid the non-fast-forward error.

现在从 'git staging' 视图,提交和推送。由于 Git/Eclipse 现在知道您已将远程源更改合并到您的主服务器中,因此您应该避免非快进错误。

回答by Don Kirkby

I also find it confusing to resolve merge conflicts in EGit. When I am ready to commit some changes to a shared repository, the steps I have learned are as follows:

我还发现在 EGit 中解决合并冲突令人困惑。当我准备向共享存储库提交一些更改时,我学到的步骤如下:

  1. Right click on the project and choose Team: Commit....
  2. Review my changes to check that I'm not committing any changes I made accidentally or unrelated changes that I forgot about. Write up the commit message while I'm reviewing the changes.
  3. I'm optimistic, so I start by clicking the Commit and Push button. If no one else has pushed any changes, I'm done. If someone has, then the commit succeeds, and the push gets rejected.
  4. Right click on the project and choose Team: Pull. If there are no conflicts, then choose Team: Push to Upstream and I'm done.
  5. If there are conflicts, look through the package explorer to see which files are conflicted. Right click on each conflicted file, and choose Team: Merge Tool. It will show all the changes in both versions of the file, with any conflicts shown in red. Click on the button to merge all non-conflict changes, then edit any red sections manually. There's also a button to show a three way merge that includes the common ancestor.
  6. Save the changes to the file. If you want, you can compare it to HEAD to see what changes you are making on top of the changes you just pulled.
  7. Right click on the file and choose Team: Add to Index to tell EGit that you've finished merging that file. To me, this is the least intuitive step, but the git command line also uses the add command to show that a merge is finished.
  8. Repeat for any other conflicted files.
  9. When all the files are merged, right click on the project and choose Team: Rebase: Continue Rebase. If there are more conflicted commits, go back to dealing with conflicts.
  10. When the rebase is complete, run your tests to see that the rebase didn't break anything.
  11. Right click on the project and choose Team: Push to Upstream.
  1. 右键单击项目并选择团队:提交...。
  2. 查看我的更改以检查我没有提交任何我不小心所做的更改或我忘记的无关更改。在我查看更改时写下提交消息。
  3. 我很乐观,所以我首先单击 Commit and Push 按钮。如果没有其他人推动任何更改,我就完成了。如果有人这样做了,那么提交成功,推送被拒绝。
  4. 右键单击项目并选择 Team: Pull。如果没有冲突,则选择 Team: Push to Upstream 就完成了。
  5. 如果存在冲突,请查看包资源管理器以查看哪些文件存在冲突。右键单击每个冲突的文件,然后选择 Team: Merge Tool。它将显示文件的两个版本中的所有更改,任何冲突都以红色显示。单击按钮合并所有非冲突更改,然后手动编辑任何红色部分。还有一个按钮可以显示包含共同祖先的三向合并。
  6. 保存对文件的更改。如果需要,您可以将其与 HEAD 进行比较,以查看您在刚刚拉取的更改之上所做的更改。
  7. 右键单击该文件并选择 Team: Add to Index 以告诉 EGit 您已完成该文件的合并。对我来说,这是最不直观的步骤,但 git 命令行还使用 add 命令来显示合并已完成。
  8. 对任何其他冲突文件重复此操作。
  9. 合并所有文件后,右键单击项目并选择 Team: Rebase: Continue Rebase。如果有更多冲突提交,请返回处理冲突。
  10. 当 rebase 完成后,运行您的测试以查看 rebase 没有破坏任何东西。
  11. 右键单击该项目并选择 Team: Push to Upstream。

回答by Tonino

This guide was helpful for me http://wiki.eclipse.org/EGit/User_Guide#Resolving_a_merge_conflict.

本指南对我很有帮助 http://wiki.eclipse.org/EGit/User_Guide#Resolving_a_merge_conflict

UPDATED Just a note on that about my procedure, this is how I proceed:

更新只是关于我的程序的说明,这就是我的处理方式:

  1. Commit My change
  2. Fetch (Syncrhonize workspace)
  3. Pull
  4. Manage Conflicts with Merge Tool (Team->Merge Tool) and Save
  5. Add each file to stage (Team -> Add to index)
  6. Now the Commit Message in the Stage window is prefilled with "Merged XXX". You can leave as it is or change the commit message
  7. Commit and Push
  1. 提交我的更改
  2. 获取(同步工作区)
  3. 使用合并工具(团队-> 合并工具)管理冲突并保存
  4. 将每个文件添加到阶段(团队 -> 添加到索引)
  5. 现在 Stage 窗口中的 Commit Message 预填充了“Merged XXX”。您可以保持原样或更改提交消息
  6. 提交和推送

It is dangerous in some cases but it is very usefull to avoid to use external tool like Git Extension or Source Tree

在某些情况下这是危险的,但避免使用外部工具(如 Git Extension 或 Source Tree)非常有用

回答by B.Kaatz

I know this is an older post, but I just got hit with a similar issue and was able to resolve it, so I thought I'd share.

我知道这是一个较旧的帖子,但我刚刚遇到了类似的问题并且能够解决它,所以我想我会分享。

(Update: As noted in the comments below, this answer was before the inclusion of the "git stash" feature to eGit.)

更新:如以下评论中所述,此答案是在将“git stash”功能包含到 eGit 之前。)

What I did was:

我所做的是:

  1. Copy out the local copy of the conflicting file that may or may not have any changes from the version on the upstream.
  2. Within Eclipse, "Revert" the file to the version right before the conflict.
  3. Run a "Pull" from the remote repository, allowing all changes to be synced to the local work directory. This should clear the updates coming down to your filesystem, leaving only what you have left to push.
  4. Check the current version of the conflicting file in your work directory with the copy you copied out. If there are any differences, do a proper merge of the files and commit that version of the file in the work directory.
  5. Now "Push" your changes up.
  1. 复制冲突文件的本地副本,该副本可能会或可能不会对上游版本进行任何更改。
  2. 在 Eclipse 中,将文件“还原”到冲突之前的版本。
  3. 从远程存储库运行“Pull”,允许将所有更改同步到本地工作目录。这应该清除归结到您的文件系统的更新,只留下您要推送的内容。
  4. 使用您复制的副本检查工作目录中冲突文件的当前版本。如果有任何差异,请正确合并文件并在工作目录中提交该版本的文件。
  5. 现在“推送”您的更改。

Hope that helps.

希望有帮助。

回答by user1583465

Just right click on a conflicting file and add it to the index after resolving conflicts.

只需右键单击有冲突的文件,并在解决冲突后将其添加到索引中。

回答by Michael M

An earlier process of resolving conflicts (through the staging view) in Eclipse seemed much more intuitive several years ago, so either the tooling no longer functions in the way that I was used to, or I am remembering the process of resolving merge conflicts within SVNrepositories. Regardless, there was this nifty "Mark as Merged" menu option when right-clicking on a conflicting file.

几年前在 Eclipse 中解决冲突的早期过程(通过暂存视图)似乎更加直观,所以要么工具不再以我习惯的方式运行,要么我记得在SVN中解决合并冲突的过程存储库。无论如何,当右键单击冲突文件时,有一个漂亮的“标记为合并”菜单选项。

Fast forward to 2019, I am using the "Git Staging" view in Eclipse (v4.11). Actually, I am using STS(Spring Tool Suite 3.9.8), but I think the Git Staging view is a standard Eclipse plugin for working with Java/Spring-based projects. I share the following approach in case it helps anyone else, and because I grow weary or resolving merge conflicts from the GIT command line. ;-)

快进到 2019 年,我在 Eclipse (v4.11) 中使用了“ Git Staging”视图。实际上,我使用的是STS(Spring Tool Suite 3.9.8),但我认为 Git Staging 视图是用于处理基于 Java/Spring 的项目的标准 Eclipse 插件。我分享以下方法,以防它对其他人有所帮助,并且因为我感到厌倦或从 GIT 命令行解决合并冲突。;-)

Because the feature I recall is now gone (or perhaps done differently with the current version of GIT and Eclipse), here are the current steps that I now follow to resolve merge conflicts through Eclipse using a GIT repository. It seems the most intuitive to me. Obviously, it is clear from the number of responses here that there are manyways to resolve merge conflicts. Perhaps I just need to switch to JetBrains IntelliJ, with their three-way merge tool.

因为我记得的功能现在已经消失(或者可能与当前版本的 GIT 和 Eclipse 不同),以下是我现在遵循的当前步骤,以使用 GIT 存储库通过 Eclipse 解决合并冲突。这对我来说似乎是最直观的。显然,从这里的响应数量可以看出,解决合并冲突的方法有很多。也许我只需要使用他们的三向合并工具切换到 JetBrains IntelliJ

  1. Double-click on the offending file
  2. A "Text Compare" interface appears, with side-by-side views of the conflicting commits.
  3. Identify which view is the local state of the file, or the most recent commit.
  4. Make changes to the local window, either adding or redacting the changes from the offending commit.
  5. When the desired set of changes has been reviewed and updated, right-click on the unstaged file.
  6. Click the "Add to Index" option, and your file will be added to the staged changes.
  7. This also removes the conflicting file from the unstaged list, thus indicating that it has been "marked as merged"
  8. Continue this process with each additional file in conflict.
  9. When you have reivewed all conflicting files, confirm that all desired files (including additional changes) are staged.
  10. Add an appropriate commit message.
  11. Commit and push the "merged" files to the origin repository, and this officially marks your files as merged.
  1. 双击有问题的文件
  2. 出现“文本比较”界面,其中包含冲突提交的并排视图。
  3. 确定哪个视图是文件的本地状态,或最近的提交。
  4. 对本地窗口进行更改,添加或编辑来自违规提交的更改。
  5. 审核并更新所需的更改集后,右键单击未暂存的文件。
  6. 单击“添加到索引”选项,您的文件将被添加到暂存更改中。
  7. 这也会从 unstaged 列表中删除冲突文件,从而表明它已被“标记为已合并”
  8. 对每个附加的冲突文件继续此过程。
  9. 审核完所有冲突文件后,确认所有所需文件(包括其他更改)都已暂存。
  10. 添加适当的提交消息。
  11. 提交并将“合并”的文件推送到原始存储库,这正式将您的文件标记为已合并。

NOTE: Because the menu options are not intuitive, a number of things can be misleading. For example, if you have saved any updates locally, and try to reopen the conflicting file to confirmand that the changes you made have persisted, confusion may result since the original conflict state is opened... not your changes.

注意:由于菜单选项不直观,许多内容可能会产生误导。例如,如果您在本地保存了任何更新,并尝试重新打开冲突文件以确认您所做的更改是否仍然存在,则可能会导致混淆,因为原始冲突状态已打开……而不是您的更改。

But once you add the file(s) to the index, you will see your changes there.

但是一旦您将文件添加到索引中,您就会在那里看到您的更改。

I also recommend that when you pull in changes that result in a merge conflict, that you "stash" your local changes first, and then pull the changes in again. At least with GIT, as a protection, you will not be allowed to pull in external changes until you either revert your changes or stash them. Discard and revert to the HEAD state if the changes are not important, but stash them otherwise.

我还建议,当您拉入导致合并冲突的更改时,您首先“隐藏”本地更改,然后再次拉入更改。至少对于 GIT,作为一种保护,在您还原更改或隐藏它们之前,您将不被允许引入外部更改。如果更改不重要,则丢弃并恢复到 HEAD 状态,否则将它们隐藏起来。

Finally, if you have just one or two files changing, then consider pulling them into separate text files as a reference, then revert to the HEAD and then manually update the file(s) when you pull changes across.

最后,如果您只更改了一两个文件,请考虑将它们拉入单独的文本文件作为参考,然后恢复到 HEAD,然后在拉动更改时手动更新文件。

回答by ashawki

This approach is similar to "stash" solution but I think it might be more clear:

这种方法类似于“stash”解决方案,但我认为它可能更清楚:

  1. Your current local branch is "master" and you have local changes.
  2. After synchronizing, there are incoming changes and some files need to be merged.
  3. So, the first step is to switch to a new local branch (say "my_changes"):
    • Team -> Switch To -> New branch
      • Branch name: my_changes (for example)
  4. After switching to the new branch, the uncommitted local changes still exist. So, commit all your local uncommitted changes to the new branch "my_changes":
  5. Switch back to the local "master" branch:
    • Team -> Switch To -> master
  6. Now, when you choose Team -> Synchronize Workspace, no "merge" files are expected to appear.
  7. Select Team -> Pull
    • Now the local "master" branch is up to date with respect to the remote "master" branch.
  8. Now, there are two options:
    • Option 1:
      • Select Team -> Merge to merge the original local changes from "my_changes" local branch into "master" local branch.
      • Now commit the incoming changes from the previous step into the local "master" branch and push them to the remote "master" branch.
    • Option 2:
      • Switch again to "my_changes" local branch.
      • Merge the updates from the local "master" branch into "my_changes" branch.
      • Proceed with the current task in "my_changes" branch.
      • When the task is done, use Option 1 above to update both the local "master" branch as well as the remote one.
  1. 您当前的本地分支是“master”并且您有本地更改。
  2. 同步后,有传入更改,需要合并一些文件。
  3. 所以,第一步是切换到一个新的本地分支(比如“my_changes”):
    • 团队 -> 切换到 -> 新分支
      • 分支名称:my_changes(例如)
  4. 切换到新分支后,未提交的本地更改仍然存在。因此,将所有本地未提交的更改提交到新分支“my_changes”:
  5. 切换回本地的“master”分支:
    • 团队 -> 切换到 -> 主
  6. 现在,当您选择 Team -> Synchronize Workspace 时,预计不会出现“合并”文件。
  7. 选择团队 -> 拉
    • 现在本地“master”分支相对于远程“master”分支是最新的。
  8. 现在,有两种选择:
    • 选项1:
      • 选择 Team -> Merge 将原始本地更改从“my_changes”本地分支合并到“master”本地分支。
      • 现在将来自上一步的传入更改提交到本地“master”分支并将它们推送到远程“master”分支。
    • 选项 2:
      • 再次切换到“my_changes”本地分支。
      • 将本地“master”分支的更新合并到“my_changes”分支。
      • 继续“my_changes”分支中的当前任务。
      • 任务完成后,使用上面的选项 1 更新本地“主”分支和远程分支。

回答by Som

As it's an issue we face more than often, below are the steps to resolve it.

由于这是我们经常遇到的问题,以下是解决它的步骤。

  1. Open the Git perspective. In the Git Repository view, go to on BranchesLocalmasterand right click → Merge...

  2. It should auto select Remote Tracking→ *origin/master. Press Merge.

  3. Launch stage view in Eclipse.

  4. Double click the files which initially showed conflict

  5. In the conflict merge view, by selecting the left arrow for all the non-conflict + conflict changes from left to right, you can resolve all the conflicts.

  6. Save the merged file.

  7. Do a Team → pull from Eclipse again.

  1. 打开 Git 透视图。在 Git Repository 视图中,转到BranchesLocalmaster并右键单击 → Merge...

  2. 它应该自动选择远程跟踪→ *origin/master。按Merge

  3. 在 Eclipse 中启动舞台视图。

  4. 双击最初显示冲突的文件

  5. 在冲突合并视图中,通过从左到右选择所有非冲突+冲突更改的左箭头,您可以解决所有冲突。

  6. 保存合并的文件。

  7. 做一个团队 → 再次从 Eclipse 中提取。

You are all set :)

你都准备好了:)

回答by chrisinmtown

To resolve the conflicts, use Git stash to save away your uncommitted changes; then pull down the remote repository change set; then pop your local stash to reapply your uncommitted changes.

要解决冲突,请使用 Git stash 来保存您未提交的更改;然后下拉远程仓库变更集;然后弹出您的本地存储以重新应用您未提交的更改。

In Eclipse v4.5 (Mars) to stash changes (a relatively recent addition, wasn't in previous EGit) I do this: right-click on a top-level Eclipse project that's in Git control, pick Team, pick Stashes, pick Stash Changes; a dialog opens to request a stash commit message.

在 Eclipse v4.5 (Mars) 中存储更改(相对较新的添加,不在以前的 EGit 中)我这样做:右键单击 Git 控制的顶级 Eclipse 项目,选择Team,选择Stashes,选择藏匿变化;将打开一个对话框以请求存储提交消息。

You must use the context menu on a top level project! If I right click on a directory or file within a Git-controlled project I don't get the appropriate context menu.

您必须在顶级项目中使用上下文菜单!如果我右键单击 Git 控制的项目中的目录或文件,我将无法获得相应的上下文菜单。

回答by Hirein

GIT has the most irritating way of resolving conflicts (Unlike svn where you can simply compare and do the changes). I strongly feel git has complex conflict resolution process. If I were to resolve, I would simply take another code from GIT as fresh, add my changes and commit them. It simple and not so process oriented.

GIT 具有解决冲突的最烦人的方式(与 svn 不同,您可以简单地比较并进行更改)。我强烈觉得 git 有复杂的冲突解决过程。如果我要解决,我只需从 GIT 中获取另一个代码作为新代码,添加我的更改并提交它们。它简单而不是面向过程。