Skip to content

Instantly share code, notes, and snippets.

@sebva
Created September 5, 2018 08:50
Show Gist options
  • Save sebva/72b8d7c1704cbf59a13c20cb50d71efd to your computer and use it in GitHub Desktop.
Save sebva/72b8d7c1704cbf59a13c20cb50d71efd to your computer and use it in GitHub Desktop.
Moving a directory from one repo to another
git clone <repourl>/repo-1.git
git clone <repourl>/repo-2.git
# Start in the source repo
cd repo-1
git remote rm origin # delete link to original repository to avoid any accidental remote changes
git filter-branch --subdirectory-filter dir-to-move -- --all # dir-to-move is being moved to another repo. This command goes through history and files, removing anything that is not in the folder. The content of this folder will be moved to root of the repo as a result.
# This folder has to be moved to another folder in the target repo. So, move everything to another folder.
git filter-branch --index-filter \
'git ls-files -s | sed "s-\t\"*-&NEWSUBDIR/-" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info &&
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD
# Now switch to target repo and fetch everything from source.
git clone repo-2
git remote add master ../repo-1/
git pull master master --allow-unrelated-histories # --allow-unrelated-histories may not be known by old versions of git
git push # push everything to remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment