Git 创建一个只有指定目录及其历史记录的新分支,然后推送到新的存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9971332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git create a new branch with only a specified directory and its history then push to new repository
提问by terrace
I'd like to create a new branch in my repo that only includes files from a specific directory in master and its historythen push that branch to a new repository.
我想在我的 repo 中创建一个新分支,它只包含 master 中特定目录中的文件及其历史记录,然后将该分支推送到新存储库。
...or something equivalent – for instance it may be possible to push a directory to a new repository to a new repo without creating a branch.
...或类似的东西 - 例如,可以将目录推送到新存储库到新存储库,而无需创建分支。
So far I think the following will work, but I wonder if there's a more streamlined way.
到目前为止,我认为以下方法可行,但我想知道是否有更简化的方法。
1 - Create an empty branch:
1 - 创建一个空分支:
git symbolic-ref HEAD refs/heads/<new-branch>
rm .git/index
git clean -fdx
2 - Checkout a directory from master:
2 - 从 master 检出目录:
git checkout master <paths> ...
git add <paths> ...
git commit
3 - Push branch to new remote:
3 - 将分支推送到新的远程:
git push -u <remote-URL> <new-branch>
4 - Then, in the new repo, merge branch with master:
4 - 然后,在新的 repo 中,将分支与 master 合并:
git checkout -t origin/<new-branch>
git checkout master
git merge <new-branch>
git branch -d afterimage
git branch -d -r afterimage
I'm attempting to do something equivalent to Detach subdirectory into separate Git repository, but without the git filter-branch
mess.
我正在尝试做一些相当于Detach 子目录到单独的 Git repository 的事情,但没有git filter-branch
混乱。
回答by knittl
Why don't you want to use git filter-branch
? It's been built specifically for tasks such as the one you want.
你为什么不想使用git filter-branch
?它专为诸如您想要的任务而构建。
git branch subdir_branch HEAD
git filter-branch --subdirectory-filter dir/to/filter -- subdir_branch
git push git://.../new_repo.git subdir_branch:master
This will give you only the contents of your dir/to/filter
in a new repository, including all of its history and nothing more. Since you will only filter the newly created branch, the rest of your repository is left untouched. You can delete the subdir_branch
afterwards.
这将只为您dir/to/filter
提供新存储库中的内容,包括其所有历史记录,仅此而已。由于您只会过滤新创建的分支,因此您的存储库的其余部分保持不变。subdir_branch
之后可以删除。