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

发布于 2025-01-22 21:58:03 字数 141 浏览 1 评论 0原文

假设我有一个带有三个文件夹A,B和C的GIT存储库。我想创建一个使用仅文件夹C(没有A,B)的新Git存储库,但保留了历史记录。基本上,我想要一个带有文件夹A,B删除的新git回购,并从历史记录中删除了其中的更改;一个只有文件夹C的存储库和更改文件夹内容的内容的提交。

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. Basically, I want a new git repo with folders A,B deleted and changes in them removed from the history too; a repo with just folder C and the commits which changed the contents of folder C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2025-01-29 21:58:03

假设当前的存储库foo结构是

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

使其

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

以任何一种方式使

C
└── .git

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

当前的分支重写,并且只保留了触摸C的提交(Commor SHA1值更改)。

将新分支推到新存储库。

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

Suppose the current repository foo structure is

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).

Push the new branch to the new repository.

git push <path_to_new_repo> HEAD:refs/heads/<branch>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文