[英]Git: Get a subfolder from a repository to a new one without losing history
我有多个分支的应用程序的git
存储库。 源树由几个目录组成。 例如:
main_folder
|--> .git
|--> dir0
|--> dir1
|--> dir2
不幸的是,从开发开始,我就没有使用git-submodules
或git-subtree
。 现在,我想将其中一个目录(例如dir0
作为新的独立应用程序移动到新的存储库中。当然,如果可能的话,我想保留相应目录的历史记录和分支。
所以我有两个问题:
尝试git subtree split
。 这可以拆分一个子文件夹并保留历史记录。
有关如何使用它,请参考手册页。
使用git splits
它基本上使用相同的git filter-branch
方法,但界面更简单。
git splits
。 我基于jkeating的解决方案将其创建为git扩展。 #change into your repo's directory cd /path/to/repo #checkout the branch git checkout MyOldBranch #split directory or directories into new branch git splits -b MyNewBranch dir1 dir2 dir2...
如果只需要将包含历史记录的目录复制到新的MyNewBranch分支,请在此处停止(完成)。
如果您希望将新分支推送到独立仓库中
在某处创建一个空的仓库。 我们假设我们在GitHub上创建了一个名为new_repo
的空new_repo
,其路径为: git@github.com:simpliwp/new_repo.git
推送到新的仓库。 #add a new remote origin for the empty repo so we can push to the empty repo on GitHub git remote add origin_new_repo git@github.com:simpliwp/new_repo.git #push the branch to the new repo's master branch git push origin_new_repo new_repo:master
将新创建的远程仓库克隆到新的本地目录中
#change current directory out of the old repo cd /path/to/where/you/want/the/new/local/repo #clone the remote repo you just pushed to git clone git@github.com:simpliwp/new_repo.git
我结束了git filter-branch
方法,这也是Github提到的。
步骤如下。
cd
到新目录 假设我们要创建一个仅包含分支master
的dir0
的新存储库,则相应的git
命令应为:
git filter-branch --prune-empty --subdirectory-filter dir0 master
这将删除除dir0
之外的所有其他文件和目录。 此外,它将删除与“ dir0”无关的提交,这非常方便。
在原始存储库中,只需删除目录dir0
并提交。
你可以做什么!
一种。 删除dir1并提交。
湾 为dir1创建一个新的存储库。
C。 将其作为子模块添加到主项目中,它将作为子模块供您使用。
编辑-1
给你用例,我建议子模块是做到这一点的方法! 您可以使用git log --pretty="%H" dir-1 | xargs -n 1 git format-patch -1
git log --pretty="%H" dir-1 | xargs -n 1 git format-patch -1
创建提交补丁,以更改dir-1中的“独立应用程序”。
现在,使用初始存储库创建一个新的存储库,其中初始存储库具有空提交。 您可以使用git clone git://github.com/mudassirazvi/EMPTY_COMMIT
克隆类似的东西,然后依次逐个应用补丁。 (您可以通过提供gitk dir1
或git log --oneline dir1
来查看顺序。
现在您的存储库已准备好历史记录,将其添加为子模块
PS:如果您的提交在同一提交中没有多个目录,这很简单。 如果有任何提交,请使用git rebase -i
编辑这些提交,以使所有“ dir1”提交都独立存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.