Skip to content

Instantly share code, notes, and snippets.

@olegch
Last active December 13, 2015 20:58
Show Gist options
  • Save olegch/4973819 to your computer and use it in GitHub Desktop.
Save olegch/4973819 to your computer and use it in GitHub Desktop.
Commands/sequence useful for git repository refactoring
#!/bin/bash
# Delete everything except some files:
find . -not \( -path "./.git" -o -path "./.git/*" -o -path "./pom.xml" \) -delete
# checkout all branches you want to have:
for i in develop br1 br2 br3; do git branch -t $i origin/$i; done
# automatically find all remote branches:
for i in `git show-ref | awk '/ refs\/remotes\/origin\// { sub(/refs\/remotes\/origin\//,""); print $2 }' | grep -v '^HEAD$'`; do git branch -t $i origin/$i; done
# remove origin just in case not to push accidentally:
git remote rm origin
# filter-branch - remove files except some
git filter-branch --tag-name-filter cat --prune-empty --tree-filter 'find . -not \( -path "./.git" -o -path "./.git/*" -o -path "./pom.xml" \) -delete' -- --all
# or filters-branch - only leave one subdir
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC -- --all
# cleanup
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment