Create new git repo from the subdirectory of an existing git repo


Create a git repository from the subdirectory of an existing git repository. Furthermore, preserve the log of only that subdirectory.

  • Within private_repo, there is a folder called repo-specific, which contains the git hooks. These git hooks are written in Python for the Bioconductor git server. The hooks check each push that comes into each Bioconductor package and run validity checks.
  • Now, there is no reason why this folder should not be open-source. It has no private information or sensitive code that the world should not see or share with the right open-source license.
  • This directory repo-specific has a git log that we need to preserve while creating a new repository. However, the entire log of the private_repo should not become available. The log of the whole repository has commit messages that record sensitive information.
|- .git/
|- file.txt
|- subdirectory1/
|- ...
|- repo-specific/
| |- sub_sub_directory/
| |- ...
| |- sub_file.txt


  1. Clone the entire private_repo in a temporary location, with
$ git clone
$ git filter-branch --subdirectory-filter repo-specific -- --all
$ git remote --set-url origin \
$ git remote -v 
origin (fetch)
origin (push)
$ git push — set-upstream origin master


As we can see in Github, there is a new repository Bioconductor/repo-specific. The repository has an open-source LICENSE and small file providing some information. The process of creating a new repository from a subdirectory is relatively straight forward and documented in the advanced git commands section in Github’s documentation.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store