Git create a new branch with only a specified directory and its history then push to new repository
Why don't you want to use git filter-branch
? It's been built specifically for tasks such as the one you want.
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.
Related videos on Youtube
terrace
Updated on June 04, 2022Comments
-
terrace almost 2 years
I'd like to create a new branch in my repo that only includes files from a specific directory in master and its history then push that branch to a new repository.
...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:
git symbolic-ref HEAD refs/heads/<new-branch> rm .git/index git clean -fdx
2 - Checkout a directory from master:
git checkout master <paths> ... git add <paths> ... git commit
3 - Push branch to new remote:
git push -u <remote-URL> <new-branch>
4 - Then, in the new repo, merge branch with 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.-
Greg Hewgill about 12 years...but you actually need to use
git filter-branch
to extract the history for the subdirectory. Without that, you just get files. -
CharlesBI really don't see the interest of not following post you link. Doing it the way you propose works, but is brute force, and it won't clean your history or repository data.
-
-
Sebastien Lorber over 2 yearsWhat I'd like is to be able to create github.com/slorber/docusaurus-starter from a monorepo subfolder (github.com/facebook/docusaurus/tree/main/examples/classic), without any history, in a single command (done by a script or CI). Nothing interactive or manual.
-
knittl over 2 years@SebastienLorber use
git archive
to export a (nested) tree from a git repository. For details, please ask a new question (or find an existing one). -
JoenMarz about 2 years@knittl what if I want to filter more than one directory? let's say
dir/to/filter1
anddir/to/filter2
in my new sub-branch. How to do it? -
knittl about 2 years@JoenMarz
--subdirectory-filter
will make a single directory the new root directory of your repository. If you want to delete all other directories, you might want to use--index-filter
(check the man page of filter-branch for details)