有时,一项功能是作为一个项目的一部分开发的,但它的发展速度如此之快,以至于有必要成为它自己的、独立的项目。
本教程介绍如何将 Git 存储库的一部分拆分为自己的专用 Git 存储库,仅保留与正在提取的子项目相关的历史记录。
提取修订历史记录
- 使用Git的filter-branch功能仅提取子项目的Git历史记录:
git filter-branch -f --prune-empty --subdirectory-filter <subdir>Where
<subdir>is the folder containing the subproject’s source code.更新 Maven 构建
假设您使用 Maven 来构建子项目:
- 将 <scm> 部分添加到 pom.xml 以反映新远程存储库的 URL(请参阅example):
vi pom.xml git commit -m 'Add SCM location' pom.xml -
将旧的 <parent> 替换为新的,例如 pom-scijava、pom-imagej 或 pom-fiji(参见 example),或者完全删除 <parent>。
-
在 pom.xml 中添加 <developers> 部分以指示项目开发人员(参见example)。如果需要且相关,您还可以添加 <contributors>。
- 确保项目仍在构建:
mvn clean package - 添加(或调整).gitignore 文件(请参阅example)。
推送更改
- 确保所有更改看起来都不错:
git status git diffThis is good advice in general: check
git statusandgit diffevery time before you commit, to prevent making a fool out of yourself. - 提交所有内容,提及重写历史的父项目的提交(参见example):
git add . && git commit -s -
在某处为新项目创建一个新存储库——我们建议GitHub。
- 将您的存储库与远程存储库连接:
git remote set-url origin git@github.com:my-org/my-new-projectWhere git@github.com:my-org/my-new-project is the remote URL for the new project’s dedicated repository.
- 将生成的历史记录推送到项目的新存储库:
git push -u origin main更改任何在线资源
- 编辑相关网页以反映新的 Git 存储库位置
- 更新该项目的任何其他已知链接