Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save squirrel532/e74901ce94a57b9847ccd26232c5edf7 to your computer and use it in GitHub Desktop.
Save squirrel532/e74901ce94a57b9847ccd26232c5edf7 to your computer and use it in GitHub Desktop.
Use git filter-branch to move all projects files to a subdir and rewrite all commits
  1. Clone project

  2. Checkout all branches that contain the files that should be moved

  3. Delete the remote

  4. Run the filter-branch command:

    git filter-branch --tree-filter 'mkdir -p /path/to/tmp; mv * /path/to/tmp; mkdir subdir; mv /path/to/tmp/* subdir/' --tag-name-filter cat --prune-empty -- --all
    • All files are first copied to a temporary dir and move from there to the new destination
    • Existing tags are updated
    • Empty commits are ignored
    • The filters are applied to all branches
  5. Add the remote

  6. Push (force) all branches and tags back to remote:

    git push -f --all
    git push -f --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment