我有一些文件,从根目录移动到了lib/
文件夹中。同时我也对这些文件进行了重大更改。
Git历史记录显示旧文件已被删除,新文件已添加。但即使文件存在大量差异,我仍希望它们被视为同一文件。有没有可能回头告诉Git这个文件是移动的,而不是删除和添加?
我有一些文件,从根目录移动到了lib/
文件夹中。同时我也对这些文件进行了重大更改。
Git历史记录显示旧文件已被删除,新文件已添加。但即使文件存在大量差异,我仍希望它们被视为同一文件。有没有可能回头告诉Git这个文件是移动的,而不是删除和添加?
Git不跟踪复制和重命名。相反,它查看文件内容并决定是重命名还是复制。
这有优点和缺点,但承认使用版本控制的人通常不会通过版本控制系统执行基本的shell命令,如mv、rm或cp。这也是因为Git将文件内容和名称分别存储(blob vs tree对象)。所以Git猜测。这也是为什么没有git-cp
的原因。
幸运的是,Git认为是复制或重命名的阈值是可配置的。git-log
和git-diff
都接受-M
。
-M[<n>], --find-renames[=<n>] If generating diffs, detect and report renames for each commit. For following files across renames while traversing history, see --follow. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file's size). For example, -M90% means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn't changed. Without a % sign, the number is to be read as a fraction, with a decimal point before it. I.e., -M5 becomes 0.5, and is thus the same as -M50%. Similarly, -M05 is the same as -M5%. To limit detection to exact renames, use -M100%. The default similarity index is 50%.
不幸的是,这会影响所有文件,没有办法将其作为存储库的一部分保存。