Skip to content

Instantly share code, notes, and snippets.

@tamalsaha
Last active May 25, 2019 22:35
Show Gist options
  • Save tamalsaha/7be6fdd48a52a4bc4ab0da5f7cb2ace3 to your computer and use it in GitHub Desktop.
Save tamalsaha/7be6fdd48a52a4bc4ab0da5f7cb2ace3 to your computer and use it in GitHub Desktop.
Splitting a subfolder out into a new repository without changing project root directory
# If you want to change project root, follow
# https://help.github.com/en/articles/splitting-a-subfolder-out-into-a-new-repository
# More advanced stuff: https://manishearth.github.io/blog/2017/03/05/understanding-git-filter-branch/
# ---------------------------------------------------------------------------------------------------
## STEPS for Splitting a subfolder out into a new repository without changing project root directory
# clone the repo
git clone [email protected]:stashed/stash.git docs
# checkout all the release branches, so that filter-branch can rewrite them
git checkout release-0.1
git checkout release-0.2
git checkout release-0.3
git checkout release-0.4
git checkout release-0.5
git checkout release-0.6
git checkout release-0.7
git checkout release-0.8
# remove unreferenced blobs from git repo
# ref: https://stackoverflow.com/a/14728706/244009
git remote rm origin
rm -rf .git/refs/original/ .git/refs/remotes/ .git/*_HEAD .git/logs/
git for-each-ref --format="%(refname)" refs/original/ | xargs -n1 --no-run-if-empty git update-ref -d
# after this `git reflog` should be empty
# How to use git filter-branch without changing project root?
# https://stackoverflow.com/a/56300480/244009
git filter-branch --prune-empty --tag-name-filter cat --index-filter '
git read-tree --empty
git read-tree --prefix=docs/ $GIT_COMMIT:docs/
' -- --all -- docs
## Installer - move hack/deploy to the top and then remove all else
git filter-branch -f --prune-empty --tag-name-filter cat --tree-filter 'mv hack/deploy deploy 1>/dev/null 2>/dev/null; true' -- --all
git filter-branch -f --prune-empty --tag-name-filter cat --index-filter "git rm -rf --cached --ignore-unmatch files_to_delete" -- --all
# Upload to new repo
$ git remote add origin [email protected]:stashed/docs.git
$ git push --all origin -u
$ git push --tag origin -u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment