18

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.

Community
  • 1
  • 1
terrace
  • 754
  • 2
  • 9
  • 21
  • 3
    ...but you actually *need* to use `git filter-branch` to extract the history for the subdirectory. Without that, you just get files. – Greg Hewgill Apr 02 '12 at 05:44
  • 1
    I 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. – CharlesB Apr 02 '12 at 05:55

1 Answers1

30

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.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • And what if I don't want the history, just a single commit? – Sebastien Lorber Sep 03 '21 at 13:47
  • @SebastienLorber what exactly do you mean? If you want a directory at a certain commit, then checkout the commit/switch to the commit and copy the directly. Or run the filter-branch command from the answer and then rebase--interactive to remove everything else – knittl Sep 03 '21 at 21:30
  • 1
    What I'd like is to be able to create https://github.com/slorber/docusaurus-starter from a monorepo subfolder (https://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. – Sebastien Lorber Sep 21 '21 at 14:50
  • @SebastienLorber use [`git archive`](https://git-scm.com/docs/git-archive) to export a (nested) tree from a git repository. For details, please ask a new question (or find an existing one). – knittl Sep 21 '21 at 16:31
  • @knittl what if I want to filter more than one directory? let's say `dir/to/filter1` and `dir/to/filter2` in my new sub-branch. How to do it? – JoenMarz Feb 11 '22 at 23:21
  • @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) – knittl Feb 12 '22 at 18:41