如何在git的历史记录中更改文件的路径?

Car*_*arl 4 git history repository

这就是我所拥有的 - 我的代码的git repo:

projects
       |-proj1 (no git repo here yet)
             |-subproj1 <- current git repo here
Run Code Online (Sandbox Code Playgroud)

这就是我想要的 - 一个git repo,它正在跟踪一个使用我的代码的新项目:

projects
       |-proj1 <-git repo moved to here, but still tracking files in subproj1
             |-subproj1 (no git repo here)
Run Code Online (Sandbox Code Playgroud)

我想保持历史记录不变,因此新的存储库将引用比原始文件更深的文件.什么是最无痛苦的方法呢?

Jör*_*tag 12

可以使用该git filter-branch命令重写历史记录.实际上,将目录树移动到子目录中是git filter-branch联机帮助页中给出的剪切和粘贴准备示例之一:

git filter-branch --index-filter '
  git ls-files -s |
  sed "s-\t\"*-&subproj1/-" |
  GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
  mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE
' HEAD
Run Code Online (Sandbox Code Playgroud)

  • 我正在尝试做类似的事情,并注意我的临时索引文件没有被写入(https://gist.github.com/60d14f45a0b2fb98e641).是否有任何正当理由没有写入临时索引? (2认同)