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

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

问题描述

关于在 Git 中重命名文件的问题 去过之前问过,但我无法找到解决我的具体问题的方法.

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.

例如:

% 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天全站免登陆