Skip to content

Instantly share code, notes, and snippets.

@greuze
Created April 10, 2019 09:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greuze/9aaefc41e266a7336bb97cea953e0c7d to your computer and use it in GitHub Desktop.
Save greuze/9aaefc41e266a7336bb97cea953e0c7d to your computer and use it in GitHub Desktop.
Move folder to other repo with history
# Clone a fresh copy of origin repo and enter into it
git clone <giturl-repoA>
cd <repoA>
# Optional, to avoid pushing to the wrong remote repository
git remote rm origin
# From the repo source, remove all the files and history ourside the folder
git filter-branch --subdirectory-filter <folder-name> -- --all
# Optional, move all the files previously in the folder to a new one, as they are in the repo root now, and commit them (no push)
mkdir <directory-new-name>
mv * <directory-new-name>
git add .
git commit
# Clone a fresh copy of destination repo (could use a previously existing one) and enter into it
git clone <giturl-repoB>
cd <repoB>
# Add a new remote pointing to local path with repo A
git remote add repoA <local-path-repoA>
# Optional, create a new branch to merge into
git checkout -b <destination-branch>
# Pull all commits from local repo A
git pull repoA master --allow-unrelated-histories
# Remove local remove, not required anymore
git remote rm repoA
# Push to remote (origin) with same branch name as local
git push origin HEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment