了解 git-filter-branch 与 update-index 的用法

Understand git-filter-branch usage with update-index

我一直在尝试破译使用 here 重写索引的 git-filter-branch 命令,以便所有 repo 内容都在一个新目录中,然后用于将其移动到另一个仓库的子目录。关注具体命令:

git filter-branch --index-filter '
        git ls-files -s |
        sed "s,\t,&'"$dir"'/," |
        GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info &&
        mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
    ' HEAD

这似乎基本上将索引从 /folder1/path/to/file 更改为 'newrootfolder'/folder1/path/to/file

问题:

GIT_INDEX_FILErepository environment variables

之一

the path to the index file (non-bare repositories only).

该过滤器命令和示例是 introduced in June 2007, with this patch

GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info 

该部分强制 git 重新创建一个新的索引文件,其中包含重命名的文件夹。

mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"

创建新索引后,它可以替换旧索引。


请注意当前选项(在python中,所以跨平台)是使用git filter-repo, which replaces the old obsolete git filter-branch or BFG

git filter-repo --path <olddirectory>/ --path-rename <olddirectory>/:<newdirectory>/

示例:

If you want two directories to be renamed (and maybe merged if both are renamed to the same location), use --path-rename; for example, to rename both cmds/ and src/scripts/ to tools/:

git filter-repo --path-rename cmds:tools --path-rename src/scripts/:tools/