简体   繁体   English

如何从现有存储库创建新的 git 存储库,仅选择存储库中的特定文件夹?

[英]How to create a new git repository from an existing repository, selecting only specific folders in the repo?

Let's say I have a git repository with three folders A, B, and C. I want to create a new git repository with just folder C ( without A, B ) but with the history preserved.假设我有一个 git 存储库,其中包含三个文件夹 A、B 和 C。我想创建一个新的 git 存储库,其中只有文件夹 C(没有 A、B)但保留了历史记录。 Basically, I want a new git repo with folders A,B deleted and changes in them removed from the history too;基本上,我想要一个新的 git 回购,其中文件夹 A、B 已删除,并且其中的更改也从历史记录中删除; a repo with just folder C and the commits which changed the contents of folder C.一个只有文件夹 C 和更改文件夹 C 内容的提交的回购协议。

Suppose the current repository foo structure is假设当前存储库foo结构是

foo
├── A
├── B
├── C
└── .git

To make it be让它成为

foo
├── C
└── .git

# backup the current branch.
# this can be omitted because
# git filter-branch automatically
# makes a backup as refs/original/refs/heads/<branch>
git branch <backup_branch>

# rewrite the current branch, removing A and B from the history
git filter-branch -f --prune-empty --tree-filter 'rm -rf A B'

# if anything goes wrong
# reset to the original status
git reset refs/original/refs/heads/<branch> --hard
# or
git reset <backup_branch> --hard

To make it be让它成为

C
└── .git

git filter-branch -f --subdirectory-filter C

In either way, the current branch is rewritten and only the commits that have touched C are preserved (the commit sha1 values are changed).无论哪种方式,当前分支都会被重写,并且只保留触及 C 的提交(提交 sha1 值已更改)。

Push the new branch to the new repository.将新分支推送到新存储库。

git push <path_to_new_repo> HEAD:refs/heads/<branch>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM