如何合并二进制文件?

Lou*_*ise 15 git

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

所以我现在做的是:

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
Run Code Online (Sandbox Code Playgroud)

但是有更简单的方法吗?

CB *_*ley 22

我不太确定你的测试用例是什么.你似乎正在移动gui.bin,然后把它放回原来的样子......

通常,二进制文件不需要合并,您只想从一个地方或另一个地方选择一个最终版本.在真正必须合并的地方,你需要一个自定义合并工具,或者使用某种编辑器和大量的人工干预.

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

git add -u
git reset -- dontcommit.dat
git commit
Run Code Online (Sandbox Code Playgroud)

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

git checkout --ours binary.dat
git add binary.dat
Run Code Online (Sandbox Code Playgroud)

或者你要合并的分支:

git checkout --theirs binary.dat
git add binary.dat
Run Code Online (Sandbox Code Playgroud)


jsp*_*cal 12

你可以使用内置的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.
Run Code Online (Sandbox Code Playgroud)

示例.gitattributes行:

*.bin -crlf -diff merge=binary
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

[merge "binmerge"]
  name = my binary merge script
  driver = binmerge.sh %O %A %B
Run Code Online (Sandbox Code Playgroud)

这可以检查冲突的文件与应该总是被本地版本覆盖的文件列表...

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

*.bin -crlf -diff merge=binmerge
Run Code Online (Sandbox Code Playgroud)

将调用binmerge.sh来处理合并.它基本上可以做类似的事情:

#!/bin/sh
echo "Performing merge of binary object ($1, $2, $3)"
touch $2
exit 0
Run Code Online (Sandbox Code Playgroud)