40

我有一个 git repo,目前我是唯一一个使用它的人。我想获取根文件夹下的所有文件和文件夹,并将它们放在一个新文件夹中。

当前结构:

主要->
  源代码
  资源

新结构:

主要->
  应用程序1
    源代码
    资源

有没有办法让文件不会丢失它们的 git 历史记录?

4

3 回答 3

53

TL;DR

Go ahead: move your files and directories around. Just make sure you don't make any edits to files in the same commit as your directory restructuring.

Why It Works

Git is a content tracker, not a file tracker. If you move/rename files, but make no other changes to the content of those files, then Git just rewrites the tree objects. The file blobs are not changed by directory operations; directory location information is stored separately in tree objects.

Actual rename detection is handled by diffing the blobs and trees, and looking for a configurable percentage of similarity between files. This means that Git doesn't really store moves or renames directly; it computes them from differences between commits.

The upshot of all this is that your history is not tied to a particular filename or directory structure. This works really well for most use cases, but stands in contrast to systems like Bazaar that track renames as first-class operations.

于 2012-05-31T08:14:53.103 回答
7

您只需移动文件,Git 就会(或应该)注意到移动已经发生。它将保留历史。

如果由于某种原因它没有--find-copies-harder注意到移动,您可以尝试使用带有选项的 diff。

于 2012-05-31T06:15:33.600 回答
3

当我移动或重命名一些文件时,我也丢失了历史记录。但我试图接触TortoiseGitSourceTree应用程序。

当我尝试使用 git bash 命令时,我发现我可以看到历史记录。

$Git log

在网上花了几个小时了解原因后,终于找到了答案。

Tortoise git 默认设置,向我们展示了实际的文件历史记录。我的意思是,如果你重命名,如果你移动任何文件或文件夹,你就看不到历史记录。要查看历史记录,您必须单击 TortoiseGit 设置上的以下复选框。

在此处输入图像描述

于 2019-03-25T13:59:48.283 回答