[英]Git: move files in history too
is it possible with Git's tools to move files into a new folder while modifying its full history, as if the files had been there from their first add? 是否有可能使用Git的工具将文件移动到新的文件夹,同时修改其完整的历史记录,就好像文件是从他们的第一次添加?
I came up on this after merging some repos together: I moved the files from several repos to distinct folders inside one "super" repo, however the merged history behaves very bad with git rebase
and git svn
tools as totally different files may collide at their old locations of course. 在合并了一些repos之后我想出了这个:我将文件从几个repos移动到一个“超级”repo中的不同文件夹,但是合并的历史表现非常糟糕,
git rebase
和git svn
工具因为完全不同的文件可能会碰撞他们的当然是旧址。
So now this got the job done: 所以现在这完成了工作:
git filter-branch --tree-filter '(ls -A; mkdir TheSubdir; echo TheSubdir) | xargs mv'
Strange but nice: the .git dir stays where it should; 奇怪但很好:.git目录停留在应有的位置; maybe git is preventing its movement somehow?
也许git以某种方式阻止它的运动?
In order to modify your full history, you will need to rewrite every commit. 要修改完整的历史记录,您需要重写每次提交。
git filter-branch
is the best way to do this. git filter-branch
是执行此操作的最佳方式。 In particular, for your case, git filter-branch --tree-filter some-command
will check out every version of your history, run your command in which you can modify the working tree as you see fit (such as moving the directories in question), and then rewrite that commit to contain the new contents of the tree. 特别是,对于您的情况,
git filter-branch --tree-filter some-command
将检查您的历史记录的每个版本,运行您可以根据需要修改工作树的命令(例如移动目录)问题),然后重写该提交以包含树的新内容。
The command in the accepted answer will fail for files/directories containing spaces etc. The following version is safe: 对于包含空格等的文件/目录, 接受的答案中的命令将失败。以下版本是安全的:
git filter-branch -f --tree-filter 'mkdir TheSubdir; \
find . -maxdepth 1 -name TheSubdir -prune -o -name . -true -o -print0 | \
xargs -0 mv -t TheSubdir'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.