重新组织项目文件夹后丢失 git 历史记录
大约一个月前,我进行了一次提交,其中涉及创建新文件夹和子文件夹,并在它们之间移动源代码文件。从那时起,我第一次查看我的历史记录,并意识到 git 已经“丢失”了历史记录,因为原始文件被删除然后重新添加,我想 - 即当我在 git GUI 中查看文件时(它是在保密协议下,所以我不能直接讨论它,但例如,这个存储库在 GitHub 上也被破坏了,GitHub 清楚地显示它检测到提交是一系列动作。)它只显示每个文件回到项目文件夹时的历史记录。被重组。
阅读了几个问题后(让 Git 确认以前移动的文件, 如何让 git 将已删除的文件和新文件标记为文件移动?),我只是比开始时更迷失了。从这些答案看来,我根本无法解决这个问题?我真的很感激这里的任何帮助。
I made a commit about a month ago that involved me creating new folder and sub-folders and moving my source code files in between them. I just was looking through my history for the first time since then and realized git has 'lost' history since the original files were deleted and then re-added, I suppose - i.e. when I'm viewing a file in my git GUI (it's under NDA so I can't discuss it directly, but for example, this repository is broken on GitHub too. GitHub clearly shows its detecting the commit as a series of moves.) it only shows history for each file back to when the project folder was reorganized.
After reading a few questions ( Getting Git to Acknowledge Previously Moved Files, How to make git mark a deleted and a new file as a file move?), I'm simply more lost than when I started. It sounds like from those answers that I won't be able to fix this at all? I'd really appreciate any help here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Git 中没有文件移动的概念。
某些工具(例如 GitHub)会考虑包含已删除的名为 X 的文件和已在文件移动中创建的名为 X 的文件的提交。
根据 Linus Torvalds 的说法,文件移动只是重构的一个特例;因此 Git 不会区别对待。与许多其他情况一样,这种特殊情况的处理留给了更高级别的工具(例如前端)。
有关此主题的更多信息,请查看此答案< /a> 来自莱纳斯·托瓦兹。
In Git there's no the concept of file move.
Some tools, like GitHub, consider a commit containing a file named X that has been deleted and a file named X that has been created a file move.
According to Linus Torvalds, file move is just a special case of refactoring; for this reason Git will not treat it differently. Handling of this special case, like many others, is left to higher-level tools (such as frontends).
For more info on this topic, check this answer from Linus Torvalds.
据我所知,您想要:
请参阅 http://git-scm.com/docs/git -log 了解详细信息。我不确定这是否是你想要的;但对于 git,git 跟踪内容而不是文件。问题是确定该信息确实非常昂贵,并且假设通常您不需要它......
As far as I see it you want:
See http://git-scm.com/docs/git-log for details. I am not sure if that is what you want; but in case of git, git tracks content not files. The problem is that determining that information is really expensive and it is assumed that normally you don't need it...
如果你想在 git 中移动文件夹,你可以使用 git mv。
我的存储库根目录中有一堆文件夹,并且想将它们移动到两个子目录中,因此我使用 **mkdir 创建了两个新目录。
然后我一次将文件和文件夹移动到新目录中,如下所示:
等等。
我有一种情况,我想将其中一个目录重命名为 src,我这样做了:
结果在一组看起来像这样的文件中:
完成后,我创建了一个名为“reorganized”的新分支,这样我就不会干扰其他开发人员在主分支中所做的工作。他继续处理这些文件,当他将新的更改推送到 master 时,我将这些更改拉取并合并到我的分支中,一切都按照我的希望进行。已移动的文件从其原始位置正确引用,并收到其提交。
If you want to move folders around in git, you can use git mv.
I had a bunch of folders in the root of my repository, and wanted to move them into two subdirectories, so I created the two new directories using **mkdir.
Then I moved files and folders one at a time into the new directories like this:
etc.
I had a case where I wanted to rename one of the directories to src and I did it like this:
This resulted in a set of files that looked like this:
After I was finished, I created a new branch called "reorganized" so I wouldn't interfere with work another developer was doing in the master branch. He continued to work on the files, and as he pushed new changes to master, I pulled and merged the changes into my branch, and everything worked as I had hoped. Files that had been moved were referenced properly from their original locations, and received their commits.
您是否尝试设置 config
diff.renames
?注意:要跟踪单个文件的重命名历史记录,您需要使用“
git log -p --follow file
”。Did you try settings the config
diff.renames
?Note: to follow history of a single file across renames, you need to use "
git log -p --follow file
".如果您实际上缺少历史记录中的修订(从您的问题中尚不完全清楚实际缺少的内容)并且只有一个月左右,您可能还有时间 - 查看引用日志;它会保留您签出的每一份参考资料的副本,因此,如果您犯了错误,您可以找回之前的资料。
默认保留 reflog 条目 90 天,它们会被 git gc 过期。
If you're actually missing revisions from your history (it's not completely clear from your question what's actually missing) and it's only been around a month, you probably still have time -- look at the reflog; it keeps a copy of every reference you have checked out, so if you make a mistake you can get back what you had before.
The default is to keep reflog entries for 90 days, they are expired by
git gc
.我能想到几种可能性。如果事情只是被打乱,但不跟踪,那么我认为这是在两次不同的提交中完成的......一次删除,然后一次重新添加。在此之前的提交中创建一个新分支,将接下来的两个提交作为一个提交加载,然后追加之后的所有内容。
如果情况并非如此,您可能需要使用 git log -M -C 来查看提交
I can think of a few possibilities. If things were just shuffled around, but don't track, then I presume that this was done in two different commits... one to remove and then one to re-add. Create a new branch in the commit before that, load in the next two commits as one commit, and then append everything after that.
If that is not the case, you may need to look at the commits with
git log -M -C
我将运行一个遍历所有对象的脚本。您需要先解压所有包文件。该脚本将检查类型,如果是提交,您将看到您是否是作者。然后检查日期。列出这些并 grep 查找所需的文件名。一旦你有了感兴趣的提交,创建一个分支
,看看你是否拥有你想要的一切,
从这里你可能想要做一些过滤分支、嫁接和/或变基以再次链接历史记录。
I would run a script that goes through all the objects. You would need to unpack all the pack files first. The script would check the type, if it is a commit you would see if you are the author. Then check the date. List those and grep for the file name that you want. Once you have the commit of interest, create a branch with
and see if you have all that you want with
from here you may want to do some filter branching, grafting and/or rebasing to link up the history again.