让Git遵循重命名和编辑的文件 [英] Getting Git to follow renamed and edited files

查看:102
本文介绍了让Git遵循重命名和编辑的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关在Git中重命名文件的问题 been 问过之前,但我无法解决我的具体问题。

Questions about renaming files in Git have been asked before, but I can't work out a solution to my specific problem.

我移动并编辑了多个文件(我没有使用 git mv - 不幸的是现在已经太晚了)。现在我想要它,当我的同事从我的存储库中取出时,对相同的文件进行了自己的编辑(不移动它们),它成功地将我的更改与他在文件的新位置合并。要成功合并,Git显然需要知道这些文件是相同的。

I have moved and edited multiple files (I didn't use git mv - unfortunately it's now too late for that). Now I want it so when my colleague pulls from my repository, having made his own edits to those same files (without moving them), it successfully merges my changes with his in the file's new location. To successfully merge, Git clearly needs to know that these are the same files.

Git是否足够聪明,可以自己完成这项工作?这似乎很难相信。如果是这样,我怎么能确定一个特定的文件移动会被Git拾取 - 即使内容已经改变了?

Is Git clever enough to work this out on its own? It seems hard to believe. And if so, how can I be sure that a particular file move will be picked up by Git - even if the contents has changed?

推荐答案

Git实际上并不会在存储库中跟踪重命名,它使用差异启发式来确定您是否将文件重命名为另一个文件。也就是说,如果你 git mv 一个文件,然后完全替换这些内容,那么 不会被认为是重命名,而是不是需要使用 git mv 来检测重命名。

Git doesn't actually track renames in the repository, it uses a diff heuristic to determine if you renamed a file to another. That is to say, if you git mv a file and then replace the contents completely, it is not considered a rename, and you do not need to use git mv for it to detect renames.

例如: p>

For example:

% mv d.txt e.txt
% git rm d.txt
rm 'd.txt'
% git add e.txt
% git commit -m"rename without git mv"
[master f70ae76] rename without git mv
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename d.txt => e.txt (100%)
% git diff --summary --find-renames HEAD~1 HEAD
 rename d.txt => e.txt (100%)

同样, git mv 并不意味着文件将被重命名,它仍然会使用diff算法:

Similarly, git mv doesn't mean that the file will be renamed, it will still use the diff algorithm:

% git mv e.txt f.txt
% echo "Completely replacing f.txt" > f.txt
% git add f.txt
% git commit -m"git mv doesn't help here"
[master 068d19c] git mv doesn't help here
 2 files changed, 1 insertion(+), 14 deletions(-)
 delete mode 100644 e.txt
 create mode 100644 f.txt
% git diff --summary --find-renames HEAD~1 HEAD
 delete mode 100644 e.txt
 create mode 100644 f.txt

这篇关于让Git遵循重命名和编辑的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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