我目前有一个如下所示的文件结构:
# C:RepositoriesVisualStudioMyProject
.git # repository is one level above clientapp and includes other folders/paths than clientapp
afolder
clientapp
file1
file2
现在我想将clientapp
文件夹移到一个新位置,并将其重命名为frontend
。然后我只想为frontend
创建一个新的存储库。在这里之前没有问题:
# inside C:RepositoriesMyProjectfrontend (no more VisualStudio in the path!)
.git
<all the frontend folders and files>
现在问题来了:当然,我现在丢失了原始clientapp
(现在为:frontend
(文件夹的历史记录,因为我创建了一个新的存储库。有什么方法可以将与原始clientapp
文件夹有关的提交复制到我的frontend
新存储库中?
TLDR:将原始存储库筛选到要保留的文件夹,然后从新存储库中提取。
-
下载并使用
git filter-branch
官方推荐的git filter repo。*Windows用户-请参阅在Windows 上安装git-filter-repo
-
将存储库过滤到相关文件(确保之前有备份!(:
git filter-repo --path clientapp
-
将客户端应用程序中的文件移动到根
git filter-repo --subdirectory-filter clientapp/
-
转到您的新存储库,在原始存储库中添加一个远程:
git remote add repo-A <path to original repo, e.g. ../repo-A>
-
将此分支中的文件和历史记录拉入存储库B(仅包含要移动的目录(。
git pull repo-A master
-
删除与存储库A的远程连接。
git remote rm repo-A
-
从新存储库推送
git push
-
删除原始存储库的副本(仅包含筛选后的文件夹(。如果需要,请再次克隆它。愉快地使用具有完整提交历史记录的新存储库。
进一步阅读:关于如何将文件从一个存储库移动到另一个存储库来保存git历史记录的中型文章,尤其是关于将文件合并到新存储库B的部分。