简体   繁体   English

Git:也在历史中移动文件

[英]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 rebasegit 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.

相关问题 是否可以在 Git 中移动/重命名文件并维护其历史记录? - Is it possible to move/rename files in Git and maintain their history? Git /如何在简单的“移动文件”后检索文件历史记录 - Git / How to retrieve the files history after a simple “move files” Git重写历史记录将文件夹和文件移动到父文件夹 - Git rewrite history move folders and files up to parent folders 在移动目录上保留Git历史记录 - Retain Git history on move directory Git和二进制文件的历史记录 - Git and binary files history Git 历史作为常规文件 - Git history as regular files 如何使用`git format-patch`和`git am`将文件从一个git repo移动到另一个保存历史记录 - How to move files from one git repo to another preserving history using `git format-patch`and `git am` 是否可以将一堆文件从一个git存储库移动到另一个git存储库,同时保留(大多数)历史记录? - Is it possible to move a bunch of files from one git repository to another while preserving (most) history? git,确保在保留历史记录的同时移动/重命名文件的方法 - git, sure-fire way to move/rename files while keeping the history 如何在通过SVN创建的GIT存储库中移动文件并保留历史记录? - How do you move files in a GIT repository created from SVN and keep the history?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM