我有一个拥有许多Visual Studio项目的仓库,我想将它们提取到自己的解决方案和仓库中,但我不想失去历史记录。
是否有一种方法可以从当前仓库创建一个具有完整历史记录但仅包含我选择的文件夹的仓库?
我可以克隆仓库并删除我不想要的内容,但我希望节省一些空间。
是否有一种方法可以从当前仓库创建一个具有完整历史记录但仅包含我选择的文件夹的仓库?
我可以克隆仓库并删除我不想要的内容,但我希望节省一些空间。
git filter-branch
现在建议使用其他替代方案!(在 git 版本 2.26 中检查)
$ git filter-branch --prune-empty --subdirectory-filter ...
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead. See the
filter-branch manual page for more details; to squelch this warning
...
除了 @kowsky 的回答,你也可以使用 git-filter-branch 来完成相同的操作,具体如下:
git clone https://github.com/USERNAME/REPOSITORY-NAME
cd REPOSITORY-NAME
git remote rm origin
git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME BRANCH-NAME
git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY-NAME.git
git push -u origin BRANCH-NAME
git filter-branch --tree-filter 'rm status' HEAD
的东西,但我总是得到 rm: cannot remove 'status': No such file or directory
错误。 - Paulo MorgadoFOLDER-NAME
是您想要保留的文件夹名称。 - Arpit Aggarwalcd ~/repository/path
git log --pretty=email --patch-with-stat --reverse --full-index --binary -- path/to/file_or_folder > /tmp/patc
如有需要,进行路径替换
sed -i -e 's/deep\/path\/that\/you\/want\/shorter/short\/path/g' /tmp/patch
提交到新的仓库
cd ~/another_repository/path
git am < /tmp/patch
警告!重写的历史将对所有对象具有不同的对象名称,并且不会与原始分支合并。您将无法轻松地将重写的分支推送和分发到原始分支之上。如果您不知道全部含义,请不要使用此命令,并且在可能的情况下避免使用它,如果一个简单的单个提交就足以解决您的问题。
- kowsky