git - 创建新的 git 分支,仅包含某些文件的完整历史记录

标签 git refactoring git-filter-branch

我正在进行重构,最终将把“核心模型”从我围绕它构建的 API 中分离出来。

我有当前(简化的)目录结构:

/root
|-model_core.py
|-model_configuration.py
|-model_frontend.py
|-plot_model.py
|-cool_use_for_model.py

如何创建一个新分支 model-core,其中包含 model_core.pymodel_configuration.py ,以及当前分支的完整提交历史记录?

最佳答案

您有几种选择:

http://blogs.atlassian.com/2014/04/tear-apart-repository-git-way/
https://lostechies.com/johnteague/2014/04/04/using-git-subtrees-to-split-a-repository/

主要的 2 个选项是:

git filter-branch

git subtree split --prefix=lib -b split

如上所述,让我们详细解释每个选项

filter-branch 将循环遍历每个提交,然后您可以仅 checkout 给定的文件,而 git subteree split 在您的情况下将是更好的选择。

解释这两个选项,以便您选择您喜欢的一个。


示例代码:

过滤器分支

# Filter the master branch to your directory and remove empty commits
git filter-branch --prune-empty --subdirectory-filter YOUR_FOLDER_NAME filter_from_branch

这会将给定文件夹中的所有所需文件 check out 到当前目录


子树分割

git subtree git-subtree - Merge subtrees together or split repository into subtrees

git subtree split -P <name-of-folder> -b <name-of-new-branch>

enter image description here

关于git - 创建新的 git 分支,仅包含某些文件的完整历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184532/

相关文章:

git diff merge-base 与工作树(使用 ... 符号)

git - 如何判断当前工作目录是否被 Git 忽略?

java - Eclipse 重构 : move method inside collaborator

oop - 牺牲设计和代码质量以与现有模块集成

git - 将 master merge 到 dev 分支的正确方法

c# - 在 asp.net mvc3 中重构 Controller 的操作

git 有效替换邮箱地址

git - filter-branch 删除多余的目录

git 按子文件夹拆分存储库并保留所有旧分支

linux - Git 存档行结尾与克隆不同