Skip to content

Instantly share code, notes, and snippets.

@jamietsao
Last active September 2, 2015 18:07
Show Gist options
  • Select an option

  • Save jamietsao/4e797a297b2df667ea0c to your computer and use it in GitHub Desktop.

Select an option

Save jamietsao/4e797a297b2df667ea0c to your computer and use it in GitHub Desktop.
Moving directory/files in Git while preserving history
################################################################################
# Moving files from one repo to another:
# http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/
# filter one directory
git clone <git repository A url>
cd <git repository A directory>
git remote rm origin
git filter-branch --subdirectory-filter <directory 1> -- --all
mkdir <directory 1>
mv * <directory 1>
git add .
git commit
# pull into another repo
git clone <git repository B url>
cd <git repository B directory>
git remote add repo-A-branch <git repository A directory>
git pull repo-A-branch master
git remote rm repo-A-branch
################################################################################
# In order to remove individual files:
# http://stackoverflow.com/a/10524661
git filter-branch -f --prune-empty --index-filter "git rm --cached --ignore-unmatch remove1.txt remove2.txt"
# in order to remove history for older files that are no longer part of the repo, find all files that ever existed:
git log --pretty=format: --name-only --diff-filter=A | sort -u
# keep wanted files and remove newlines
git log --pretty=format: --name-only --diff-filter=A | sort -u | grep -v "keep1.txt\|keep2.txt" | tr "\\n" " "
# then feed that list into above command
################################################################################
# In order to move files to new directories while maintaining history:
# https://gist.github.com/emiller/6769886
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment