11

背景

嗨,我正在开发本地功能分支。这个本地分支被很多小提交搞砸了。在将分支推送到远程之前,我想整理一下。

为此,我将进行交互式变基:

git rebase -i

到目前为止没有问题。

问题

现在是困难的部分:在功能开发过程中,我进行了几次重构,包括重命名移动文件。由于重命名文件,重命名文件的历史可用:

git -mv

但是当我在重命名提交之前和之后压缩提交时,历史记录就消失了,git 将更改通知为删除和添加文件。

问题是什么?

如何在不丢失文件历史记录的情况下压缩提交,包括重命名?

4

1 回答 1

7

Git does not track renames directly, it compares file contents and detects renames by similarity.

When you squash a history you put all changes of a file in one commit. The file might then change a lot compared to the previous commit. Thus it is not very similar to the previous commit and git thinks it is a delete/add.

if you want to see the history of such a file you must adjust the find-renames threshold. E.g. for 50% similarity use

git log --follow --find-rename=50 -- someFile

Similar options are also available for diff, merge and rebase. Take a look at the docs:

git rebase

rename-threshold=n Controls the similarity threshold used for rename detection. See also git-diff1 -M.

git diff

--find-renames[=n]

Detect renames. 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%.

于 2015-02-19T12:44:16.530 回答