Skip to content

Instantly share code, notes, and snippets.

@grunka
Created April 11, 2017 15:33
Show Gist options
  • Save grunka/697f9ae9a05f2c03a10f78aa1bfbda15 to your computer and use it in GitHub Desktop.
Save grunka/697f9ae9a05f2c03a10f78aa1bfbda15 to your computer and use it in GitHub Desktop.
For that time when you have something in a sub directory of a large project and want to move it to the root of its own repository and only keep the history for that code in the new repository
#!/usr/bin/env bash
path_in_old_repo=some/sub/directory
old_repo=git@github.com:user/old_repo.git
new_repo=git@github.com:user/new_repo.git
new_directory=$(echo ${new_repo} | sed 's|^.*/\(.*\)\.git$|\1|g')
git clone ${old_repo} ${new_directory}
cd ${new_directory}
git remote set-url origin ${new_repo}
git filter-branch --subdirectory-filter ${path_in_old_repo} -- --all
for tag in $(git tag); do
git tag -d ${tag}
done
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all
git gc --prune=now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment