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

前端 未结 2 1046
野趣味
野趣味 2020-11-30 19:45

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

2条回答
  •  悲哀的现实
    2020-11-30 20:19

    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
    

提交回复
热议问题