Extract one or more subdirectories from the current Git repository into a new repository, with contents flattened to the root and history rewritten so only those paths remain.
- Bash (with
set -euo pipefailsupport) - git ≥ 2.x
- git-filter-repo (install) — must be on your
PATH
git-pickdir [OPTIONS] REPODIR SUBDIRS...- REPODIR — Target directory for the new repository (must not already exist).
- SUBDIRS... — One or more repository-relative subdirectory paths to extract.
git-pickdir ../newrepo src/lib include/commonCreates ../newrepo containing only the contents of src/lib and include/common, at the root of the new repo, with history preserved for those paths.
Put git-pickdir somewhere on your PATH, e.g.:
cp git-pickdir ~/bin/
# or
sudo cp git-pickdir /usr/local/bin/The tool exits with an error if:
- Not run inside a Git repository
- Repository has no commits
REPODIRalready exists- No
SUBDIRSprovided - Any
SUBDIRdoes not exist atHEAD - Any
SUBDIRis an absolute path or contains.. - One subdir is a prefix of another (e.g.
srcandsrc/lib) - Flattening would produce a filename collision (same name from different subdirs)
git-filter-repois not installed or not inPATH- Any Git or filter-repo command fails
- No files remain after filtering
- Repo root is detected with
git rev-parse --show-toplevel; all logic is based on that root. - Copy vs clone: If the repo is “fresh” (no loose objects, no remotes), the tool uses
cp -afor speed; otherwise it usesgit clone --no-localto avoid hardlinks. - History is rewritten with
git filter-reposo only the chosen paths exist and are moved to the repository root. - The new repository is independent: no remotes, no hardlinks to the original.