我可以让`git merge`在文件更改时始终发生冲突吗? [英] Can I make `git merge` always conflict on file changes?

查看:127
本文介绍了我可以让`git merge`在文件更改时始终发生冲突吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我对你有一个简单的问题,但我无法在任何地方找到答案(我一直在寻找2天,也许我是愚蠢的?)。我正在尝试开发一个git工作流程来与我的博士顾问协作编辑LaTeX文件。我们面临的问题是由于 git merge 的行为(因为它自动合并)。我希望git在看到文件更改时发生冲突,即使它是一种加法,减法或小改变,这是可能的吗?这样我们既可以选择变化,也可以不断推动主人。



我不反对使用其他工具,但我不想创建复杂的分支系统。当2-3个人在同一个文件上工作时,我认为分支是不必要的。非常感谢你的帮助! 你必须使用 - no-commit 在这里,即使当git认为它运行良好时也可以停止合并。



关于后续的问题,您可以让git提取文件中的两个正在合并的版本有很多方法。最简单的可能是使用 git show 。 (注意,这会绕过涂抹过滤器)。



合并停止时, HEAD 将当前提交和 MERGE_HEAD 命名正在合并的提交(假定正在进行双亲合并;我从来没有用章鱼合并来试过),所以如果你有文件 foo.tex git认为它已经正确合并,并且你想自己检查它,你可以这样做:

  $ git show HEAD:foo.tex> foo.tex.head 
$ git show MERGE_HEAD:foo.tex> foo.tex.merge_head
$ vimdiff foo.tex foo.tex.head foo.tex.merge_head

这个特定的命名方案(加入 .head .merge_head 后缀)不是很聪明,如果当然,实际上有一个名为 foo.tex.head 的真实文件,所以对于一个强大的工具,您希望创建临时目录。这实际上可以使这个过程更容易,因为您可以使用 git checkout (其中不会应用涂抹过滤器)来填充临时目录。例如,这有点浪费(检查整个 HEAD MERGE_HEAD 树,而不是每个文件):

  $ hd_tree = / tmp / head。$$ mhd_tree = / tmp / merge_head。$$ 
$ index = / tmp / index。$$
$ mkdir $ hd_tree $ mhd_tree
$ GIT_INDEX_FILE = $ index GIT_WORK_TREE = $ hd_tree git checkout HEAD - 。
$ GIT_INDEX_FILE = $ index GIT_WORK_TREE = $ mhd_tree git checkout MERGE_HEAD - 。

现在对于每个文件 $ f 希望检查:

  $ vimdiff $ f $ hd_tree / $ f $ mhd_tree / $ f 
code>

全部完成后, rm -rf $ hd_tree $ mhd_tree $ index 至放弃临时工作树和虚假索引。

为了减少文件空间的浪费(但使用更多进程),请检查每个文件 $ f >,如上所述,使用 GIT_WORK_TREE 环境设置。 ( GIT_INDEX_FILE 在这里是因为 checkout 通过索引写入,你真的不想覆盖git的合并版本 .git / index 。)



[请注意,以上都未经过测试。警告emptor。]


I am hoping I have a simple question for you, but I can't find an answer anywhere (I have been searching for 2 days, maybe I am dumb?). I am trying to develop a git workflow to collaboratively edit LaTeX files with my PhD adviser. The problem we face is due to the behavior of git merge (because it merges automagically). I want git to conflict anytime it sees a file change, even if it is an addition, subtraction, or minuscule change, is this possible? This way we can both pick and choose changes and continually push to master.

I am not opposed to using another tool, but I would prefer not to create a complicated branching system. I assume branching is unnecessary when 2-3 people are working on the same file. Thank you so much for your help!

解决方案

As vcsjones noted in a comment, you pretty much have to use --no-commit here, to stop the merge even when git thinks it went well.

Regarding your subsequent question, you can get git to extract the two being-merged versions of the file in a number of ways. The simplest is probably to use git show. (Note that this bypasses smudge filters, though.)

When the merge stops, HEAD names the current commit and MERGE_HEAD names the commit being merged-in (this assumes a two-parent merge in progress; I've never tried this with an octopus merge), so if you have file foo.tex that git thinks it has merged correctly, and you want to inspect it yourself, you could do this:

$ git show HEAD:foo.tex > foo.tex.head
$ git show MERGE_HEAD:foo.tex > foo.tex.merge_head
$ vimdiff foo.tex foo.tex.head foo.tex.merge_head

This particular naming scheme (adding .head and .merge_head suffixes) is not very clever and could fail if you actually have a "real" file named foo.tex.head, of course, so for a robust tool you'd want to make temporary directories instead. This could actually make the process easier as you could use git checkout (which does apply smudge filters) to populate the temporary directories. For instance, this is a bit wasteful (checks out the entire HEAD and MERGE_HEAD trees rather than each file as you go):

$ hd_tree=/tmp/head.$$ mhd_tree=/tmp/merge_head.$$
$ index=/tmp/index.$$
$ mkdir $hd_tree $mhd_tree
$ GIT_INDEX_FILE=$index GIT_WORK_TREE=$hd_tree git checkout HEAD -- .
$ GIT_INDEX_FILE=$index GIT_WORK_TREE=$mhd_tree git checkout MERGE_HEAD -- .

Now for each file $f that you wish to hand-inspect:

$ vimdiff $f $hd_tree/$f $mhd_tree/$f

When all done, rm -rf $hd_tree $mhd_tree $index to discard the temporary work-trees, and the dummy index.

To be less wasteful of file space (but use more processes), check out each file $f one at a time as you go, using the GIT_WORK_TREE environment setting as above. (The GIT_INDEX_FILE is here because checkout writes through the index and you really don't want to overwrite git's merged version in .git/index.)

[Note that none of the above is tested; caveat emptor.]

这篇关于我可以让`git merge`在文件更改时始终发生冲突吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆