Git: move files in history too

无人久伴 提交于 2019-12-21 11:24:30

问题


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?

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.


回答1:


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; maybe git is preventing its movement somehow?




回答2:


In order to modify your full history, you will need to rewrite every commit. git filter-branch is the best way to do this. 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.




回答3:


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'


来源:https://stackoverflow.com/questions/5656404/git-move-files-in-history-too

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!