解决与二进制文件的 Git 冲突

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

Resolving a Git conflict with binary files

gitmerge-conflict-resolution

提问by Kevin Wilson

I've been using Git on Windows (msysgit) to track changes for some design work I've been doing.

我一直在 Windows (msysgit) 上使用 Git 来跟踪我一直在做的一些设计工作的更改。

Today I've been working on a different PC (with remote repo brian) and I'm now trying to merge the edits done today back into my regular local version on my laptop.

今天,我一直在另一台 PC 上工作(使用远程 repo brian),现在我正在尝试将今天所做的编辑合并回我笔记本电脑上的常规本地版本。

On my laptop, I've used git pull brian masterto pull the changes into my local version. Everything was fine apart from the main InDesign document - this shows as a conflict.

在我的笔记本电脑上,我习惯于git pull brian master将更改拉入我的本地版本。除了主 InDesign 文档之外,一切都很好 - 这显示为冲突。

The version on the PC (brian) is the latest one that I want to keep but I don't know what commands tells the repo to use this one.

PC ( brian)上的版本是我想保留的最新版本,但我不知道是什么命令告诉 repo 使用这个版本。

I tried directly copying the file across onto my laptop but this seems to break the whole merge process.

我尝试直接将文件复制到我的笔记本电脑上,但这似乎破坏了整个合并过程。

Can anyone point me in the right direction?

任何人都可以指出我正确的方向吗?

回答by mipadi

git checkoutaccepts an --oursor --theirsoption for cases like this. So if you have a merge conflict, and you know you just want the file from the branch you are merging in, you can do:

git checkout对于这种情况,接受--oursor--theirs选项。因此,如果您遇到合并冲突,并且您知道您只想要合并分支中的文件,则可以执行以下操作:

$ git checkout --theirs -- path/to/conflicted-file.txt

to use that version of the file. Likewise, if you know you want your version (not the one being merged in) you can use

使用该版本的文件。同样,如果你知道你想要你的版本(而不是被合并的版本),你可以使用

$ git checkout --ours -- path/to/conflicted-file.txt

回答by VolkA

You have to resolve the conflict manually (copying the file over) and then commit the file (no matter if you copied it over or used the local version) like this

您必须手动解决冲突(复制文件),然后像这样提交文件(无论您是复制它还是使用本地版本)

git commit -a -m "Fix merge conflict in test.foo"

Git normally autocommits after merging, but when it detects conflicts it cannot solve by itself, it applies all patches it figured out and leaves the rest for you to resolve and commit manually. The Git Merge Man Page, the Git-SVN Crash Courseor thisblog entry might shed some light on how it's supposed to work.

Git 通常在合并后自动提交,但是当它检测到它自己无法解决的冲突时,它会应用它找到的所有补丁,剩下的由您手动解决和提交。在Git的合并手册页中,针对Git SVN速成班博客条目可能揭示它是如何工作的一些情况。

Edit:See the post below, you don't actually have to copy the files yourself, but can use

编辑:请参阅下面的帖子,您实际上不必自己复制文件,但可以使用

git checkout --ours -- path/to/file.txt
git checkout --theirs -- path/to/file.txt

to select the version of the file you want. Copying / editing the file will only be necessary if you want a mix of both versions.

选择您想要的文件版本。仅当您想要混合使用这两个版本时,才需要复制/编辑文件。

Please mark mipadis answer as the correct one.

请将 mipadis 答案标记为正确答案。

回答by RobM

You can also overcome this problem with

你也可以用

git mergetool

which causes gitto create local copies of the conflicted binary and spawn your default editor on them:

这会导致git创建冲突二进制文件的本地副本并在其上生成默认编辑器:

  • {conflicted}.HEAD
  • {conflicted}
  • {conflicted}.REMOTE
  • {conflicted}.HEAD
  • {conflicted}
  • {conflicted}.REMOTE

Obviously you can't usefully edit binaries files in a text editor. Instead you copy the new {conflicted}.REMOTEfile over {conflicted}without closing the editor. Then when you do close the editor gitwill see that the undecorated working-copy has been changed and your merge conflict is resolved in the usual way.

显然,您不能在文本编辑器中有效地编辑二进制文件。相反,您可以{conflicted}.REMOTE{conflicted}不关闭编辑器的情况下复制新文件。然后当您关闭时,编辑器git将看到未修饰的工作副本已更改,并且您的合并冲突以通常的方式解决。

回答by Joshua Flanagan

To resolve by keeping the version in your current branch (ignore the version from the branch you are merging in), just add and commit the file:

要通过将版本保留在当前分支中来解决(忽略您正在合并的分支中的版本),只需添加并提交文件:

git commit -a

To resolve by overwriting the version in your current branch with the version from the branch you are merging in, you need to retrieve that version into your working directory first, and then add/commit it:

要通过使用您正在合并的分支中的版本覆盖当前分支中的版本来解决,您需要先将该版本检索到您的工作目录中,然后添加/提交它:

git checkout otherbranch theconflictedfile
git commit -a

Explained in more detail

更详细的解释

回答by kris

mipadi's answer didn't quite work for me, I needed to do this :

mipadi 的回答对我来说不太适用,我需要这样做:

git checkout --ours path/to/file.bin

git checkout --ours path/to/file.bin

or, to keep the version being merged in:

或者,为了保持合并的版本:

git checkout --theirs path/to/file.bin

git checkout --theirs path/to/file.bin

then

然后

git add path/to/file.bin

git add path/to/file.bin

And then I was able to do "git mergetool" again and continue onto the next conflict.

然后我能够再次执行“git mergetool”并继续进行下一个冲突。

回答by kris

From the git checkoutdocs

git checkout文档

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...

--ours
--theirs
When checking out paths from the index, check out stage #2 (ours) or #3 (theirs) for unmerged paths.

The index may contain unmerged entries because of a previous failed merge. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out. Using -fwill ignore these unmerged entries. The contents from a specific side of the merge can be checked out of the index by using --oursor --theirs. With -m, changes made to the working tree file can be discarded to re-create the original conflicted merge result.

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...

--ours
--theirs
从索引中检出路径时,检出阶段 #2 ( ours) 或 #3 ( theirs) 以获取未合并的路径。

由于先前的合并失败,索引可能包含未合并的条目。默认情况下,如果您尝试从索引中检出这样的条目,检出操作将失败并且不会检出任何内容。使用-f将忽略这些未合并的条目。可以使用--ours或将来自合并特定一侧的内容从索引中检出--theirs。使用-m,可以放弃对工作树文件所做的更改以重新创建原始的冲突合并结果。

回答by Brian Webster

I came across a similar problem (wanting to pull a commit that included some binary files which caused conflicts when merged), but came across a different solution that can be done entirely using git (i.e. not having to manually copy files over). I figured I'd include it here so at the very least I can remember it the next time I need it. :) The steps look like this:

我遇到了类似的问题(想要提取包含一些在合并时导致冲突的二进制文件的提交),但遇到了一个不同的解决方案,可以完全使用 git 完成(即不必手动复制文件)。我想我会把它包括在这里,所以至少我下次需要它的时候能记住它。:) 步骤如下:

% git fetch

This fetches the latest commit(s) from the remote repository (you may need to specify a remote branch name, depending on your setup), but doesn't try to merge them. It records the the commit in FETCH_HEAD

这将从远程存储库中获取最新提交(您可能需要指定远程分支名称,具体取决于您的设置),但不会尝试合并它们。它记录了 FETCH_HEAD 中的提交

% git checkout FETCH_HEAD stuff/to/update

This takes the copy of the binary files I want and overwrites what's in the working tree with the version fetched from the remote branch. git doesn't try to do any merging, so you just end up with an exact copy of the binary file from the remote branch. Once that's done, you can add/commit the new copy just like normal.

这需要我想要的二进制文件的副本,并使用从远程分支获取的版本覆盖工作树中的内容。git 不会尝试进行任何合并,因此您最终只会得到来自远程分支的二进制文件的精确副本。完成后,您可以像往常一样添加/提交新副本。

回答by david m lee

This procedure is to resolve binary file conflicts after you have submitted a pull request to Github:

此过程用于在您向 Github 提交拉取请求后解决二进制文件冲突:

  1. So on Github, you found your pull request has a conflict on a binary file.
  2. Now go back to the same git branch on your local computer.
  3. You (a) re-make / re-build this binary file again, and (b) commit the resulted binary file to this same git branch.
  4. Then you push this same git branch again to Github.
  1. 所以在 Github 上,你发现你的 pull request 在一个二进制文件上有冲突。
  2. 现在返回到本地计算机上的同一个 git 分支。
  3. 您 (a) 再次重新制作/重新构建此二进制文件,以及 (b) 将生成的二进制文件提交到同一个 git 分支。
  4. 然后你再次将这个相同的 git 分支推送到 Github。

On Github, on your pull request, the conflict should disappear.

在 Github 上,根据您的拉取请求,冲突应该会消失。

回答by tyoc213

If the binary is something more than a dllor something that can be edited directlylike a image, or a blend file (and you don't need to trash/select one file or the other) a real merge would be some like:

如果二进制文件不仅仅是一个 dll或者可以像图像或混合文件一样直接编辑的东西(并且您不需要删除/选择一个或另一个文件),那么真正的合并将类似于:

I suggest search for a diff tool oriented to what are you binary file, for example there are some free ones for image files for example

我建议搜索一个针对你是什么二进制文件的差异工具,例如有一些免费的图像文件

and compare them.

并比较它们。

If there is no diff tool out there for compare your files, then if you have the original generatorof the bin file (that is, there exist an editorfor it... like blender 3d, you can then manually inspect those files, also see the logs, and ask the other person what you should include) and do a output of the files with https://git-scm.com/book/es/v2/Git-Tools-Advanced-Merging#_manual_remerge

如果没有用于比较文件的 diff 工具,那么如果您拥有bin 文件的原始生成器(也就是说,存在一个编辑器...例如 Blender 3d,您可以手动检查这些文件,也查看日志,并询问其他人您应该包含哪些内容)并使用https://git-scm.com/book/es/v2/Git-Tools-Advanced-Merging#_manual_remerge输出文件

$ git show :1:hello.blend > hello.common.blend $ git show :2:hello.blend > hello.ours.blend $ git show :3:hello.blend > hello.theirs.blend

$ git show :1:hello.blend > hello.common.blend $ git show :2:hello.blend > hello.ours.blend $ git show :3:hello.blend > hello.theirs.blend

回答by BoJohDoh

I've come across two strategies for managing diff/merge of binary files with Git on windows.

我遇到过两种在 Windows 上使用 Git 管理二进制文件的差异/合并的策略。

  1. Tortoise git lets you configure diff/merge tools for different file types based on their file extensions. See 2.35.4.3. Diff/Merge Advanced Settings http://tortoisegit.org/docs/tortoisegit/tgit-dug-settings.html. This strategy of course relys on suitable diff/merge tools being available.

  2. Using git attributes you can specify a tool/command to convert your binary file to text and then let your default diff/merge tool do it's thing. See http://git-scm.com/book/it/v2/Customizing-Git-Git-Attributes. The article even gives an example of using meta data to diff images.

  1. Tortoise git 允许您根据文件扩展名为不同的文件类型配置差异/合并工具。见 2.35.4.3。差异/合并高级设置http://tortoisegit.org/docs/tortoisegit/tgit-dug-settings.html。这种策略当然依赖于可用的合适的差异/合并工具。

  2. 使用 git 属性,您可以指定一个工具/命令将您的二进制文件转换为文本,然后让您的默认差异/合并工具完成它的工作。请参阅http://git-scm.com/book/it/v2/Customizing-Git-Git-Attributes。这篇文章甚至给出了一个使用元数据来区分图像的例子。

I got both strategies to work with binary files of software models, but we went with tortoise git as the configuration was easy.

我有两种策略来处理软件模型的二进制文件,但我们使用了 tortoise git,因为配置很简单。