Skip to content

Instantly share code, notes, and snippets.

@lausdahl
Last active May 18, 2022 22:02
Show Gist options
  • Save lausdahl/c32dafef219d6025424d to your computer and use it in GitHub Desktop.
Save lausdahl/c32dafef219d6025424d to your computer and use it in GitHub Desktop.
How to Extract a Subdirectory as a New git Repository
# 1. Clone the remote 
git clone monster-repository project-repository

# 2. change dir
cd project-repository

# 3. Remove the remote
git remote rm origin

# 4. Filter branch 
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter project-directory HEAD

# 5. Clean up
git reset --hard
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all
git gc --aggressive --prune=now

# 7. Add a new remote and push 
git remote add origin [url]
git push origin master

Source http://jamesreubenknowles.com/how-to-extract-a-subdirectory-as-a-new-git-repository-1566

Mowing files

# Move root into probruntime
git filter-branch -f --prune-empty --tree-filter '
mkdir -p .sub;
mv * .sub;
mv .sub probruntime
' -- --all

# Rewrite path of probruntime to ide/probruntime
git filter-branch -f --tree-filter  'mkdir -p ide; mv probruntime/ ide/' HEAD

Kill all tags

git tag -l | xargs git tag -d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment