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

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.

  • Show local commits when branch name contains slash
  • Create a new git repository from a existing local branch
  • git remote merge commit causing false dependencies in gerrit
  • Git - multiple machines per developer - committing across machines but not to main branch
  • how to list all uncommitted changes made only in current branch in git
  • How find most recent tag for current revision in Git/HG/Bzr?
  • 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.

  • Cannot make header be indexed
  • VCS Migration to Git - How is my approach?
  • List Git aliases
  • git diff - handling long lines?
  • Git - error: RPC failed; result=22, HTTP code = 401 fatal: The remote end hung up unexpectedly
  • Cloning a single path of a bare git repository
  • One Solution collect form web for “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.

    Git Baby is a git and github fan, let's start git clone.