gpt4 book ai didi

Git 压缩重命名文件的提交(保留历史记录)

转载 作者:太空狗 更新时间:2023-10-29 13:02:24 25 4
gpt4 key购买 nike

背景

您好,我在本地功能分支上工作。这个本地分支被许多小的提交搞得一团糟。在将分支推送到远程之前,我想整理一下。

为此我会做一个交互式的 rebase :

git rebase -i

目前没问题。

问题

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

git -mv

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

问题是什么?

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

最佳答案

Git 不直接跟踪重命名,它比较文件内容并通过相似性检测重命名。

压缩历史记录时,您会将文件的所有更改放在一次提交中。与上一次提交相比,该文件可能会发生很大变化。因此它与之前的提交不太相似,git 认为它是删除/添加。

如果您想查看此类文件的历史记录,您必须调整find-renames 阈值。例如。 50%相似度使用

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

diffmergerebase 也有类似的选项。查看文档:

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

关于Git 压缩重命名文件的提交(保留历史记录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28606064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com