Git create a new branch with only a specified directory and its history then push to new repository

16,056

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.

Share:
16,056

Related videos on Youtube

terrace
Author by

terrace

Updated on June 04, 2022

Comments

  • terrace
    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
      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.
    • CharlesB
      CharlesB
      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.
  • Sebastien Lorber
    Sebastien Lorber over 2 years
    What 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
    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
    JoenMarz about 2 years
    @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?
  • knittl
    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)