How can I rewrite history so that all files, except the ones I already moved, are in a subdirectory?

巧了我就是萌 提交于 2019-11-26 11:57:51

问题


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!


回答1:


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



回答2:


To wrap things up here's a short summary of what I did. The command that worked for me was:

if [ ! -e foo/bar ]; then mkdir -p foo/bar; git ls-tree --name-only $GIT_COMMIT | grep -v ^foo$ | xargs -I files mv files foo/bar || echo ""; fi

The echo command that I added at the end ensured that even when mv fails entire command will continue running. It didn't move contents of foo/bar/foo, but I can live with that.

Thanks a lot to Dan Moulding (!!!) and Jefromi for the help.



来源:https://stackoverflow.com/questions/4042816/how-can-i-rewrite-history-so-that-all-files-except-the-ones-i-already-moved-ar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!