如何更改 git 历史记录中的文件路径?
这是我所拥有的 - 我的代码的 git 存储库:
projects
|-proj1 (no git repo here yet)
|-subproj1 <- current git repo here
这是我想要的 - 一个 git 存储库,它现在正在跟踪使用我的代码的新项目:
projects
|-proj1 <-git repo moved to here, but still tracking files in subproj1
|-subproj1 (no git repo here)
我想保持历史记录完整,因此新存储库将是指比原始文件深一级的文件。最无痛的方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以使用 git filter-branch 命令来重写历史记录。事实上,将目录树移动到子目录中是
git filter-branch
联机帮助页:Rewriting history can be done with the
git filter-branch
command. In fact, moving a directory tree into a subdirectory is one of the cut&paste-ready examples given in thegit filter-branch
manpage:Git 并不擅长跟踪文件的移动,因此如果您想保留文件历史记录,则必须确保旧存储库始终将其文件放在您现在需要的子目录中
git-filter-branch 工作得足够好,但是
git-filter-repo
,它允许您以更简单的方式追溯移动文件(以及您可以使用 git-filter-branch 执行的其他操作)。复制自我自己的答案:
另请参阅如何合并两个 Git 存储库?
Git isn't exactly good at tracking files getting moved around, so if you want to preserve file history, you're going to have to make it so the old repository always had its files in the sub-directory you now need it to be in.
git-filter-branch
works decently enough, but is no longer recommended. The man page now points togit-filter-repo
, which lets you retroactively move files around (among other things you can do withgit-filter-branch
) in a much simpler way.Copied from my own answer here:
See also How do you merge two Git repositories?
只需在存储库中创建您想要的目录结构 - 即将所有文件和文件夹移动到“subproj1”文件夹。
然后暂存所有添加和删除的文件,git 会发现它们实际上是重命名的:
Just create the directory structure you want inside the repo - i.e. move all files and folders to "subproj1" folder.
Then stage all added and deleted files and git will work out that they are in fact renames: