我如何重写历史记录,以便除已移动的文件之外的所有文件都位于子目录中? [英] How can I rewrite history so that all files, except the ones I already moved, are in a subdirectory?

查看:97
本文介绍了我如何重写历史记录,以便除已移动的文件之外的所有文件都位于子目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 git 下有一个项目。有一天,我将所有项目文件从当前目录移至项目下的 foo / bar / 。我使用 git mv 来完成它。然后,我添加了一些文件,并对已存在的文件做了一些更改。



因此,现在当我查看 foo / bar / file.c ,我只能看到我在移动文件后所做的更改。



我试图以各种方式解决此问题( filter-branch 和子目录过滤器等),但没有任何帮助,所以我在这里相当堆叠。我会感谢您能给我的任何帮助。
Thanks!

解决方案

用移动的文件重写历史记录



如果您希望项目的历史记录看起来好像所有文件一直位于目录 foo / bar 中,那么您需要做一点手术。使用 git filter-branch 与树状过滤器重写提交,以便在任何地方 foo / bar 不会存在,它被创建并且所有文件都被移动到它:

  git filter-branch --prune-empty --tree-过滤'
if [! -e foo / bar];然后
mkdir -p foo / bar
git ls-tree --name-only $ GIT_COMMIT | xargs -I文件mv文件foo / bar
fi'

现在将记录历史记录就好像所有的文件都位于 foo / bar 中一样

要查看移动文件的历史记录,请执行以下操作:



如果您只想查看已在某处移动或重命名的文件的历史记录过去,然后简单地使用 - 遵循选项 git log :

  git log --follow foo / bar / file.c 


I have a project under git. One day I moved all project files from current directory to foo/bar/ under the project. I did it using git mv. Then I added some more files and did some changes to already existing files.

As a result, now when I look at the history of foo/bar/file.c, I can only see changes that I did after I moved the file.

I tried to fix this in various ways (filter-branch with subdirectory filter, etc), but nothing helped, so I am pretty stacked here. I'll appreciate any help that you can give me. Thanks!

解决方案

To rewrite the history with the files moved:

If you want the project's history to look as though all files have always been in the directory foo/bar, then you need to do a little surgery. Use git filter-branch with the "tree filter" to rewrite the commits so that anywhere foo/bar doesn't exist, it is created and all files are moved to it:

git filter-branch --prune-empty --tree-filter '
if [ ! -e foo/bar ]; then
    mkdir -p foo/bar
    git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files foo/bar
fi'

Now the history will be recorded as if all files were always located in foo/bar.

To see the history of a moved file:

If you just want to see the history of a file that has been moved or renamed at some point in the past, then simply use the --follow option to git log:

git log --follow foo/bar/file.c

这篇关于我如何重写历史记录,以便除已移动的文件之外的所有文件都位于子目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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