我想在一个git库中的两个任意文件上利用git-merge算法。下面是我的工作目录:
folder/
file1
file2file1和file2很相似,但我想看看git如何将它们合并,就像它们是同一文件的不同版本一样。换句话说,我想要这样的东西:
git merge-files file1 file2 > merge_of_file1_file2有没有办法做到这一点?
发布于 2012-02-03 12:04:07
这没有什么意义,因为您并没有提供一个共同的祖先。但是,如果您确实有一个,您可以使用:
git merge-file <current-version> <common-ancestor> <other-version>这会将结果放在当前版本的文件中;如果您希望在其他地方获得结果,请使用:
git merge-file -p <current> <common> <other> > <dest>它需要共同的祖先提供一些东西来考虑与之相关的变化。您可以通过提供空文件或存储库历史记录中某个版本的旧版本的副本来破解它,但结果的质量将取决于您选择公共祖先的程度,因为它合并了该版本和每个新版本之间的两个差异。空文件只有在两者非常相似的情况下才能正常工作(多次运行时至少有三行相同的行)。
如果没有这一点,你真正能做的就是看看它们的区别:
git diff --no-index file1 file2发布于 2012-02-03 12:09:35
对于您正在尝试做的事情:
touch file3 #empty
git merge-file file1 file3 file2这将以file3 (空文件)为基础对file1和file2进行三向合并。
请注意,这将写入file1。当然,如果不需要这样做,您可以执行以下操作:
touch file3
cp file1 merge_of_file1_file2
git merge-file merge_of_file1_file2 file3 file2发布于 2018-07-08 14:47:32
为了补充manojlds的答案,这里有一个很好的、完整的函数,您可以将它添加到.bashrc中来完成这项工作。它的好处是在“合并冲突”块中正确识别两个文件的名称。
merge() {
local ext
[ $# -ne 2 ] && echo "Error: Need exactly two args." && return 1
[[ ! -r $1 || ! -r $2 ]] && echo "Error: One of the files is not readable." && return 1
if [[ ${1##*/} =~ '.' || ${2##*/} =~ '.' ]]; then
[ ${1##*.} != ${2##*.} ] && echo "Error: Files must have same extension." && return 1
ext=.${1##*.}
fi
touch tmp$ext # use empty file as the 'root' of the merge
cp $1 backup$ext
git merge-file $1 tmp$ext $2 # will write to file 1
mv $1 merge$ext
mv backup$ext $1
rm tmp$ext
echo "Files merged into \"merge$ext\"."
}https://stackoverflow.com/questions/9122948
复制相似问题