Git:大规模重构,保留更改日志

发布于 2024-10-21 09:23:46 字数 50 浏览 2 评论 0原文

是否有可能在不丢失 Git 更改跟踪的情况下进行涉及移动和重命名许多目录的大规模重构?

Is it possible to do a massive refactor that involves moving and renaming many directories without losing Git change tracking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2024-10-28 09:23:46

git 可以很好地处理移动/重命名目录,但要跟踪这些重命名中的更改,您可能需要向您使用的任何命令添加一些额外的参数,或者设置几个配置选项。

这样做的原因是 git 仅存储每次提交时树的状态,而不是为了从一种状态移动到另一种状态而发生的更改。如果源树中有很多文件,那么您可能需要告诉 git 主动尝试查找任何重命名。同样,如果您对特定文件感兴趣,则需要明确告诉 git 搜索其过去可能的重命名。

举一个后者的例子,一个典型的例子是使用 git log -- filename 来检查特定文件的历史记录。为了告诉 git 在可能发生的任何重命名之前也查找其历史记录,您必须这样做:

git log --follow -- filename

再举一个例子, git log --stat 的有用输出可能不包括您的所有信息如果树中有许多文件,则重命名或复制,因为它需要检查所有文件对才能执行此操作。要强制 git 在使用 git log 和 git diff 时检测副本和重命名,您可以将配置选项 diff.renameLimit 至少设置为树中的文件,并将配置选项 diff.renames 设置为 copies - 这意味着检测副本和重命名。

或者,如果您不想将这些设置为配置选项,则可以使用 -M-C 选项来 git loggit 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:

git log --follow -- filename

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 using git log and git diff you can set the config option diff.renameLimit to at least the number of files in your tree, and set the config option diff.renames to copies - 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 to git log or git diff. These are described in more detail in Jakub Narębski's answer to this question:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文