Git tracks files by where they are in the directory structure. This creates unintended commits and losses of file history when renaming directories. There are two good solutions I’ve found for these issues, depending on the circumstances.
Change A Directory’s Name
Turns out Git has its own mv
command just like a unix-like OS does. So, to change the name of a directory from frog to barista, it is as simple as:
git mv frog barista
Source, and some excellent alternative solutions here: http://stackoverflow.com/questions/11183788/in-a-git-repository-how-to-properly-rename-a-directory
Rewrite Full Directory History For All Files
This is method is really drastic, as it makes it rewrites the history of the repo. It is as if the files were always there, and the move never happened. I recently did this when I wanted to use one repository as the starting point for another, but needed to move everything in the old repository down one level into a subdirectory.
In this example, all files are moved into a directory called “root”. You can use slashes to move down more levels.
git filter-branch --prune-empty --tree-filter ' if [[ ! -e root ]]; then mkdir -p root git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files root fi'