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.

  • Perforce, How to integrate a change to another branch?
  • Can I safely rebase one branch into other and then to master?
  • How can I create a branch for a non-tip revision in Mercurial?
  • Git feature branches and minor code improvements
  • How to make an existing git branch track a remote SVN branch?
  • Best strategy to distribute number into groups evenly
  • 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.

  • Buiding hibernate-orm from the source in GitHub
  • Does any faster centralized version control than SVN exists?
  • Can a “git rm -r -f <directory>” command be reverted?
  • How does rebasing master on a “remote” local branch works?
  • Why is git warning of CRLF on a file flagged as binary?
  • How to make 'git log --oneline' show which commit messages are multiline
  • 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.