Skip to content

Instantly share code, notes, and snippets.

@WimObiwan
Last active June 29, 2016 07:19
Show Gist options
  • Save WimObiwan/84e8eb8c89881d1752d94de82f554d36 to your computer and use it in GitHub Desktop.
Save WimObiwan/84e8eb8c89881d1752d94de82f554d36 to your computer and use it in GitHub Desktop.
# How-to to copy take a directory 'BuildScripts' out of a big GIT repository 'ContactCentre', retainining history
# STEP 1: Create the BuildScripts repository
# Clone the big repository, which will be stripped down to the directory BuildScripts
git clone ContactCentre BuildScripts
# Safety measure: detach from server, to prevent pushing a split back to the server by mistake
git remote rm origin
# Check which current directories you want to keep
# (using gui)
# Optionally check if there are "deleted" directories that you want to remove
$files = $( git log --diff-filter=D --summary | ?{ $_.Contains('delete') } | %{ $_ -replace '^ delete mode \d+ (.*)$', '$1' } )
# Directories
$files | %{ $_ -replace '^(.*/)[^/]+$', '$1' } | Select-Object -Unique
# Top-level directories
$files | %{ $_ -replace '^([^/]+/).*$', '$1' } | Select-Object -Unique
# Remove directory(s) that are NOT the directory you want to keep [A]
git filter-branch --index-filter 'git rm --cached --ignore-unmatch -r DirectoryToRemove Directory\ With\ Spaces\ To\ Remove' --prune-empty -- --all
# git keeps a 'backup reference' of the originals. [B] These can be removed by:
git for-each-ref --format="%(refname)" refs/original/ | %{ git update-ref -d $_ }
# (needed before re-running git filter-branch unless you use '-f' to overwrite backup)
# Steps [A] and [B] can be repeated
# Optionally move directories to top-level
git filter-branch --tree-filter 'test -d Directory && mv Directory/* . || echo Nothing_To_Do' -- --all
# (removal of the directory 'Directory' is not needed)
# Again, remove backup… (Step [B])
# Drop all dangling data
git gc --prune=now
# STEP 1: Create the BuildScripts repository
# Similar to the procedure above, but do the reverse:
# remove BuildScripts instead of all directories that are not the BuildScripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment