To rewrite the history with the files moved:
If you want the project's history to look as though all files have always been in the directory foo/bar
, then you need to do a little surgery. Use git filter-branch
with the "tree filter" to rewrite the commits so that anywhere foo/bar
doesn't exist, it is created and all files are moved to it:
git filter-branch --tree-filter 'if [[ ! -e foo/bar ]]; then mkdir -p foo/bar; git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files foo/bar; fi'
Now the history will be recorded as if all files were always located in foo/bar
. In most cases, you can probably stop and leave it at this.
The only potential minor issue is there may now be a commit in your history that introduces no changes. This would only be the case if when you originally moved the files to foo/bar
that move was the only change made in that particular commit. There's not necessarily anything wrong with this, but it is a little odd to have a commit that doesn't change anything. If this is the case, then you can simply find that commit and delete it. Probably the easiest way to do this is with git rebase -i
.
To see the history of a moved file:
If you just want to see the history of a file that has been moved or renamed at some point in the past, then simply use the --follow
option to git log
:
git log --follow foo/bar/file.c