Git:大规模重构,保留更改日志
是否有可能在不丢失 Git 更改跟踪的情况下进行涉及移动和重命名许多目录的大规模重构?
Is it possible to do a massive refactor that involves moving and renaming many directories without losing Git change tracking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
git 可以很好地处理移动/重命名目录,但要跟踪这些重命名中的更改,您可能需要向您使用的任何命令添加一些额外的参数,或者设置几个配置选项。
这样做的原因是 git 仅存储每次提交时树的状态,而不是为了从一种状态移动到另一种状态而发生的更改。如果源树中有很多文件,那么您可能需要告诉 git 主动尝试查找任何重命名。同样,如果您对特定文件感兴趣,则需要明确告诉 git 搜索其过去可能的重命名。
举一个后者的例子,一个典型的例子是使用 git log -- filename 来检查特定文件的历史记录。为了告诉 git 在可能发生的任何重命名之前也查找其历史记录,您必须这样做:
再举一个例子, git log --stat 的有用输出可能不包括您的所有信息如果树中有许多文件,则重命名或复制,因为它需要检查所有文件对才能执行此操作。要强制 git 在使用 git log 和 git diff 时检测副本和重命名,您可以将配置选项 diff.renameLimit 至少设置为树中的文件,并将配置选项
diff.renames
设置为copies
- 这意味着检测副本和重命名。或者,如果您不想将这些设置为配置选项,则可以使用
-M
或-C
选项来git log
或git diff
。 Jakub Narębski对此问题的回答中有更详细的描述:git copes fine with moving / renaming directories, but to track changes across these renames you may need to add some extra parameters to whatever command you're using, or set a couple of config options.
The reason for this is that git just stores the state of the tree at each commit rather than the changes that took place in order to move from one state to another. If you have many files in your source tree then you may need to tell git to actively try to find any renames. Similarly, if you're interested in a particular file, you'll need to tell git explicitly to search for possible renamings in its past.
To give an example of the latter, a typical case is using
git log -- filename
for examining the history of a particular file. In order to tell git to also look for its history before any rename that might have occurred, you have to do:As another example, the useful output of
git log --stat
may not include all of your renames or copies if you have many files in your tree, since it needs to check all pairs of files to do that. To force git to detect copies and renames when usinggit log
andgit diff
you can set the config optiondiff.renameLimit
to at least the number of files in your tree, and set the config optiondiff.renames
tocopies
- this means to detect copies and renames.Alternatively, if you don't want to set these as config options, you can use the
-M
or-C
options togit log
orgit diff
. These are described in more detail in Jakub Narębski's answer to this question: