删除并在另一个文件夹中重新创建后,保留文件的Git历史记录 [英] Preserve Git history of file after deleting and recreating in another folder

查看:75
本文介绍了删除并在另一个文件夹中重新创建后,保留文件的Git历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

  1. 在提交1中删除了一个名为src/GetData.cs的文件.
  2. 在提交5中创建了一个名为src/Get/GetDataNew.cs的文件.
  3. 将提交2中的文件重命名为提交7中的src/Get/GetData.cs.

1和3中的文件几乎相同,只是做了些微的更改,但是由于多次迭代,我最终创建了一个全新的文件并删除了旧文件.有什么办法可以在文件3中保存/移动文件1的git历史记录?

The files in 1 and 3 are pretty much the same with minor changes but due to several iterations, I ended up creating an entirely new file and deleting the old one. Is there any way to preserve/move git history of file 1 in file 3?

我能想到的一种方法是还原到删除旧文件然后使用

One approach that I can think of is reverting back to the commit where I deleted the old file and then using

git mv src/GetData.cs src/Get/GetData.cs

git mv src/GetData.cs src/Get/GetData.cs

将其移动到新文件夹,但是之间还有很多其他提交和更改,因此我想避免这种情况.

to move it to new folder but there have been a lot of other commits and changes in between so I'd like to avoid this.

先谢谢了.

推荐答案

Git没有文件历史记录.没有什么可以保留或还原的.

Git does not have file history. There is nothing to preserve or restore.

Git具有 commits 并提交 are 历史记录.就是这样,这就是您所拥有的全部.添加提交以添加更多历史记录.

Git has commits and commits are history. That's it—that's all you have. Add commits to add more history.

现在,提交包含文件确实如此,您可以要求Git遍历提交历史,但是 list 仅包含某些特定的提交,即那些涉及特定提交的提交按名称归档.当您这样做时,您会立即遇到名称随时间变化的文件的问题.

Now, it's true that commits contain files, and you can ask Git to walk through commit history but list only some specific commits, namely those commits that touch a particular file by name. When you do that, you immediately run into an issue with files whose names have changed over time.

对于某些文件来说,成为同一文件"意味着什么.这是一个哲学问题,这意味着它没有一个好的答案.:-)有关极端示例,请考虑我祖父的悖论斧头:我父亲替换了头部,我替换了手柄,但这是祖父的斧头;或更一般地说,是 These修斯之船.

This gets into what it means for some file to be "the same" file. This is a philosophical issue, which means it does not have a good answer. :-) For extreme examples of this, consider the paradox of my grandfather's axe: my father replaced the head and I replaced the handle, but this is my grandfather's axe; or more generally, the Ship of Theseus.

Git的答案是:如果您在 git log 命令中添加了-follow ,而您告诉它跳过(出于输出目的列出)提交并不会更改我命名的文件,Git会查看似乎已被删除的提交,并确定在父提交中是否存在带有以下内容的文件:一些具有足够内容的其他名称,以调用它为同一文件".如果内容在单个提交跃点(不是合并提交)中100%相同,那么Git会(总是)很快(很快)找到该文件.在其他情况下,您会把握机会.如果Git发现名称已更改,则Git继续执行 git log 来查找更改了一个文件的提交,但是现在它正在查找更改了以先前名称存储的文件的提交.

Git's answer is: if you add --follow to a git log command in which you have told it skip (for listing on output purpose) any commit that does not change the file I name, Git will look at commits where the file seems to have been deleted, and find out whether, in the parent commit, there is a file with some other name that has similar-enough content to call it "the same file". Git will always find this file (and quite quickly) if the content is 100% identical, across a single commit hop, that is not a merge commit. In other cases, you take your chances. If Git finds that the name changed, Git continues doing git log looking for commits that changed one file, but now it is looking for commits that changed the file as stored under the previous name.

Git的另一个答案是:您为什么在乎?每个快照都完全保留了整个状态.如果您检出提交1或提交7,为什么还要关心 src/GetData.cs 是否与 src/Get/GetData.cs 相同"文件?在快照中永久保存(或至少只要提交存在于历史记录中)的正确名称下,您将具有正确的文件内容.

Git's other answer is: Why do you care? Each snapshot completely preserves the entire state. If you check out commit 1 or commit 7, why do you care whether src/GetData.cs is "the same" file as src/Get/GetData.cs? You will have the correct file contents under the correct name as preserved forever (or at least as long as the commit exists in the history) in the snapshot.

(有时有一些需要照顾的原因,这与操作系统的文件标识概念有关.Git并不关心这些.)

(There are sometimes reasons to care, having to do with the operating system's notions of file identity. Git does not care about those.)

这篇关于删除并在另一个文件夹中重新创建后,保留文件的Git历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆