Skip to content

Instantly share code, notes, and snippets.

@morsdyce
Forked from dannyroberts/preserving-history.md
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morsdyce/07dad2abb579ddd92517 to your computer and use it in GitHub Desktop.
Save morsdyce/07dad2abb579ddd92517 to your computer and use it in GitHub Desktop.
move and rename directories in git while preserving history #git

Did you know...

...that git lets you move and rename whole directories while preserving the git history of those files?

If you run

git read-tree --prefix=$NEW_PATH -u master:$OLD_PATH
rm -rf $OLD_PATH
# ...you probably want to change some imports...
git commit -m $MESSAGE

that will move the directory tree to the new location, and, when you commit, it'll preserve this history of those files.

Things that still work:

  • git blame
  • git merge with code that branched off before the renaming (incredible!)
    • you may have to set merge.renamelimit to something higher for this to work, i.e. git config merge.renamelimit 4000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment