在两个单独的文件上运行 git merge 算法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9122948/
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
run git merge algorithm on two individual files
提问by Alexander Bird
I want to take advantage of the git-merge algorithm on two arbitrary files in a git repo. Here is my working directory:
我想在 git repo 中的两个任意文件上利用 git-merge 算法。这是我的工作目录:
folder/
file1
file2
file1 and file2 are similar, but I want to see how git would merge them as if they were different versions of the same file. In other words, I want something like this:
file1 和 file2 相似,但我想看看 git 如何合并它们,就好像它们是同一文件的不同版本。换句话说,我想要这样的东西:
git merge-files file1 file2 > merge_of_file1_file2
Is there a way to do this?
有没有办法做到这一点?
回答by Cascabel
This doesn't really make sense, because you're not providing a common ancestor. If you do have one, however, you can use:
这真的没有意义,因为您没有提供共同的祖先。但是,如果您确实有,则可以使用:
git merge-file <current-version> <common-ancestor> <other-version>
This places the results in the current version file; if you want them elsewhere, use:
这会将结果放在当前版本文件中;如果您想在其他地方使用它们,请使用:
git merge-file -p <current> <common> <other> > <dest>
It needs the common ancestor to provide something to consider changes relative to. You could hack it, by providing an empty file or a copy of an older version of one of them from the history of your repository, but the quality of results will depend on how well you select a common ancestor, since it's merging the two diffs, between that and each of the new versions. An empty file will only work well if the two are verysimilar (many runs of at least three identical lines).
它需要共同的祖先来提供一些东西来考虑相对的变化。您可以通过提供一个空文件或存储库历史记录中其中一个的旧版本副本来破解它,但结果的质量将取决于您选择共同祖先的程度,因为它合并了两个差异,在那个和每个新版本之间。一个空文件只有在两者非常相似时才能很好地工作(许多运行至少三个相同的行)。
Without that, all you can really do is look at the differences:
没有它,您真正能做的就是查看差异:
git diff --no-index file1 file2
回答by manojlds
For what you are trying to do:
对于您正在尝试做的事情:
touch file3 #empty
git merge-file file1 file3 file2
This will do a three-way merge of file1 and file2 with file3 ( empty file ) as the base.
这将以 file3(空文件)为基础对 file1 和 file2 进行三向合并。
Note that this writes to file1. You can of course do the below if that is not desired:
请注意,这将写入 file1。如果不需要,您当然可以执行以下操作:
touch file3
cp file1 merge_of_file1_file2
git merge-file merge_of_file1_file2 file3 file2
回答by Luke Davis
Just to add on to manojlds's answer, here's a nice, complete function you can add to your .bashrc
that does the job. It has the benefit of properly identifying your two files' names in the "merge conflict" blocks.
只是为了补充 manojlds 的答案,这里有一个很好的、完整的功能,您可以添加到您.bashrc
的工作中。它的好处是可以在“合并冲突”块中正确识别两个文件的名称。
merge() {
local ext
[ $# -ne 2 ] && echo "Error: Need exactly two args." && return 1
[[ ! -r || ! -r ]] && 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 backup$ext
git merge-file tmp$ext # will write to file 1
mv merge$ext
mv backup$ext
rm tmp$ext
echo "Files merged into \"merge$ext\"."
}
回答by dominic
You can also use KDiff3 for this.
您也可以为此使用 KDiff3。
- Select file one
- Select file two
- 选择文件一
- 选择文件二
And merge :)
并合并:)