首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Git与git-crypt合并

Git与git-crypt合并
EN

Stack Overflow用户
提问于 2018-01-27 18:05:25
回答 1查看 1.6K关注 0票数 1

通常,合并两个分支时,冲突会出现在文件中,如下所示:

代码语言:javascript
复制
<<<<<<<<<< HEAD

但是,我想要进行的合并有冲突,但是我不知道为什么文件没有改变,因此里面不包含任何<<<<<< HEAD

下面是我尝试过的命令:

代码语言:javascript
复制
$ md5sum myfile.txt 
76dd0814b656b23a61d7e8204cd776de  myfile.txt
$ git merge --no-ff tmp_branch
warning: Cannot merge binary files: myfile.txt (HEAD vs. tmp_branch)
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
$ md5sum myfile.txt 
76dd0814b656b23a61d7e8204cd776de  myfile.txt
$ git status
On branch master

Your branch is up to date with 'origin/master'.

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   myfile.txt

no changes added to commit (use "git add" and/or "git commit -a")

我不知道它是否会产生任何影响,但我使用git-crypt作为后端。

-编辑01 -我很确定这是由于git-地下室的一个错误。我想出了一个相当肮脏的解决方案,但在此期间,这似乎是可行的:

首先,从共同祖先提交手动复制文件:

代码语言:javascript
复制
git checkout <your commit>
cp yourfile.txt yourfile_base.txt

然后,手动复制外部分支中的文件:

代码语言:javascript
复制
git checkout <your branch>
cp yourfile.txt yourfile_other_branch.txt

最后,回到您的主要分支,并使用类似于以下内容的git merge-file

代码语言:javascript
复制
git checkout master
git merge-file yourfile.txt yourfile_base.txt yourfile_other_branch.txt

然后,您可以打开yourfile.txt,搜索字符串<<<<<并手动更正冲突!如果你有一个更快的解决办法,请告诉我!

-编辑02 --我找到了一种更快捷的方法:代替结帐和复制,您可以在一个命令中完成:

代码语言:javascript
复制
git show <your commit>:./yourfile.txt | git-crypt smudge > ./yourfile.txt<

-编辑03 --

在托瑞克的建议下,我终于找到了解决这个问题的办法:

首先,在.gitattributes中为git-crypt管理的所有文件添加一个选项merge=git-crypt

代码语言:javascript
复制
crypt/** filter=git-crypt diff=git-crypt merge=git-crypt

然后,在文件.git/config的末尾添加以下内容:

代码语言:javascript
复制
[merge "git-crypt"]
    name = A custom merge driver used to merge git-crypted files.
    driver = ./my-merge-tool.sh %O %A %B
    recursive = binary

最后,在回购my-merge-tool.sh根目录下创建一个文件,其中包括:

代码语言:javascript
复制
ancestor_decrypted="$1__decrypt"
current_decrypted="$2__decrypt"
other_decrypted="$3__decrypt"
echo ""
echo "###########################"
echo "# Git crypt driver called #"
echo "###########################"
echo ""

echo "Decrypting ancestor file..."
cat $1 | git-crypt smudge > "${ancestor_decrypted}"
echo "Decrypting current file..."
cat $2 | git-crypt smudge > "${current_decrypted}"
echo "Decrypting other file..."
cat $3 | git-crypt smudge > "${other_decrypted}"
echo ""

echo "Merging ..."
git merge-file -L "current branch" -L "ancestor branch" -L "other branch" "${current_decrypted}" "${ancestor_decrypted}" "${other_decrypted}"
exit_code=$?
cat "${current_decrypted}" | git-crypt clean > $2

echo "Removing temporary files..."
rm "${other_decrypted}" "${ancestor_decrypted}" "${current_decrypted}"

if [ "$exit_code" -eq "0" ]
then
    echo "@@@ No conflict!"
else
    echo "@@@ You need to solve some conflicts..."
fi

exit $exit_code

确保它是可执行的:

代码语言:javascript
复制
chmod +x my-merge-tool.sh

仅此而已,你现在可以合并,樱桃采摘,甚至使用你最喜欢的合并工具,像往常一样!

EN

回答 1

Stack Overflow用户

发布于 2018-01-27 19:48:42

这并不完全是git-crypt中的一个bug,它是Git合并方式与git-crypt加密方式的一个副作用。Git只看到“清理”的文件,即加密的文件。Git真的不能因此而合并任何东西。

但是,您可以提供自己的合并驱动程序,并且有一个未完成的GitHub请求声称可以这样做。它已经发布了一年多,没有任何动议(我看到你自己找到了,并注意到它对你不起作用)。它很可能只是做得不够。我还没有调查过git-地下室内部的密码,可以这么说。

合并驱动程序的适当版本将执行合并,如果结果没有冲突,则以零状态退出。然后,Git将使用生成的%A版本作为合并结果。如果文件有冲突,合并驱动程序应该在%A文件中留下冲突标记。为了使它特别好地工作(例如,为了使它与git mergetool一起工作),您甚至可能希望解密所有三个输入,并使用git update-index将它们作为第1、2和3阶段输入索引。(特别是,这将留下未加密的松散对象;您可能希望将它们存储在在合并期间创建的其他对象目录中,并有一个合并后清理步骤来删除它们,但即使这样,它们也有成为打包对象的风险。为了减少而不是消除这种风险,在创建松散对象之前,您可以运行一个git gc)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48478877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档