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.
New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With