Git 可以在不丢失历史记录的情况下重组我的文件夹吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10828267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:49:41  来源:igfitidea点击:

Can Git restructure my folders without losing history?

git

提问by Chen Kinnrot

I got a git repo current I'm the only one using it. I want to take all files and folder under root folder and put it in a new folder for example:

我有一个 git repo current 我是唯一一个使用它的人。我想把根文件夹下的所有文件和文件夹放在一个新文件夹中,例如:

current root desc

当前根目录

main ->
  src
  res

new root

新根

main ->
  app1
    src
    res

How can I do it in git on windows without losing my history?

如何在 Windows 上的 git 中执行此操作而不会丢失我的历史记录?

回答by Todd A. Jacobs

TL;DR

TL; 博士

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

Git 是一个内容跟踪器,而不是一个文件跟踪器。如果您移动/重命名文件,但不对这些文件的内容进行其他更改,则 Git 只会重写树对象。目录操作不会更改文件 blob;目录位置信息单独存储在树对象中。

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 computesthem from differences between commits.

实际的重命名检测是通过比较 blob 和 tree并寻找文件之间可配置的相似性百分比来处理的。这意味着 Git 并没有真正直接存储移动或重命名;它根据提交之间的差异计算它们。

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 Bazaarthat track renames as first-class operations.

所有这一切的结果是,您的历史记录与特定的文件名或目录结构无关。这对于大多数用例来说非常有效,但与像Bazaar这样跟踪重命名为一流操作的系统形成对比。

回答by Cameron Skinner

You can just move the files and Git will (or should) notice that the move has happened. It will keep the history.

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

If it doesn'tnotice the move for some reason you could try using the diff with --find-copies-harderoption.

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

回答by Mahmut EFE

When I moved or renamed some files, I lost histories too. But I was trying to reach on TortoiseGitand SourceTreeapplication.

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

When I tried to reach on git bash command, I figure out that I can see the history.

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

$Git log

After spending a couple of hour on the internet about the reason, Finally, I found my answer.

在互联网上花了几个小时了解原因后,最后,我找到了答案。

Tortoise git default settings, showing us just actual files history. I meant, if you rename of if you move any file or folder, you can not see history. For the seeing history, you have to click below check box on the TortoiseGit settings.

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

enter image description here

在此处输入图片说明