git 如何合并二进制文件?

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

How do I merge a binary file?

git

提问by Louise

I have a binary file in my_branch, and when I need to make changes to it, git will of course not merge it.

我在 my_branch 中有一个二进制文件,当我需要对其进行更改时,git 当然不会合并它。

So what I do now is:

所以我现在要做的是:

git checkout my_branch
# make a change to gui.bin
mv gui.bin ~/
git commit -a
mv ~/gui.bin .
git commit -a
# git rebase to 1 commit
git checkout master
git merge my_branch

But is there an easier way?

但是有没有更简单的方法呢?

回答by CB Bailey

I'm not quite sure what your test case is driving at. You seem to be moving gui.bin out of the way and then putting it back the way it was...

我不太确定你的测试用例是什么驱动的。您似乎正在将 gui.bin 移开,然后将其放回原处...

Often, binary files don't need to be merged, you just want to chose a definitive version from one place or another. Where they genuinely have to be merged you either need a custom merge tool, or use some sort of editor and lots of manual intervention.

通常,不需要合并二进制文件,您只想从一个地方选择一个确定的版本。在真正需要合并它们的地方,您要么需要自定义合并工具,要么使用某种编辑器和大量手动干预。

I notice that you use commit -ain your example. One first step to avoid unnecessary conflicts is to not commit any binaries that might be touched incidentally unless you want to commit them. If you just git addthe files you need to commit and commit without -athen this will help. Alternatively, if there is just one file that you don't want to commit you could add -uand reset it before making a commit.

我注意到你commit -a在你的例子中使用了。避免不必要冲突的第一步是不要提交任何可能会偶然触及的二进制文件,除非您想提交它们。如果您只是git add需要提交和提交的文件,-a那么这将有所帮助。或者,如果只有一个文件您不想提交,您可以add -u在提交之前重置它。

git add -u
git reset -- dontcommit.dat
git commit

When you do merge branches that have both change a binary you may want to keep one version and not the other. After a merge where git tells you that their are conflicts in your binary you can tell git to use the version in the branch that you were on like this:

当您合并同时更改二进制文件的分支时,您可能希望保留一个版本而不是另一个版本。在合并之后 git 告诉您它们在二进制文件中存在冲突,您可以告诉 git 使用您所在分支中的版本,如下所示:

git checkout --ours binary.dat
git add binary.dat

or from the branch that you are merging in like this:

或从您要合并的分支如下:

git checkout --theirs binary.dat
git add binary.dat

回答by jspcal

you could use the built-in binarymerge driver:

您可以使用内置的binary合并驱动程序:

binary: Keep the version from your branch in the work tree, but
leave the path in the conflicted state for the user to sort out.

example .gitattributes line:

示例 .gitattributes 行:

*.bin -crlf -diff merge=binary

tells git not to add line endings, not to diff, and to keep the local version

告诉 git 不要添加行尾,不要差异,并保留本地版本

http://git-scm.com/docs/gitattributes

http://git-scm.com/docs/gitattributes

that only preserves your working copy...

只保留您的工作副本...

another way is to use a custom merge driver:

另一种方法是使用自定义合并驱动程序:

[merge "binmerge"]
  name = my binary merge script
  driver = binmerge.sh %O %A %B

That could check the conflicting file against a list of files that should always be overwritten by your local version...

这可以根据应始终由本地版本覆盖的文件列表检查冲突文件...

to use a merge driver, define it in the config, then specify what paths it should be used on in .gitattributes, like so:

要使用合并驱动程序,请在配置中定义它,然后在 .gitattributes 中指定它应该使用的路径,如下所示:

*.bin -crlf -diff merge=binmerge

binmerge.sh will be called to handle the merge. it can essentially just do something like:

binmerge.sh 将被调用来处理合并。它基本上可以执行以下操作:

#!/bin/sh
echo "Performing merge of binary object (, , )"
touch 
exit 0