git.day/11 • See all at git.day
Passing a pathspec to git-log makes it possible to log only the commits that change the file or files in that given path. While pathspecs have a few custom behaviours as explored in Git of the day #10: git add :/path/to/file, it is still in essence a description of a path. This still has limitations when talking about files and folders in a Git repository, which may be moved around for various reasons.
Git keeps track of moves in terms of files being deleted in one location and created in another. This shows in commits as a rename when both deletion and addition occur in the same commit. The rename/move tracking behaviour is the same whether done manually with mv
, or with Git of the day #8: git mv. However, even if the commit has the information that the path ‘moved’, this is not part of the path itself. Therefore, a pathspec only has the concept of being the path at a specific point in time, usually now.
To make git log path/to/file
also include changes to the file when it was at a different location before being moved to path/to/file
, use the git-log option --follow
. Then,
- When Git reaches the commit that added the file, it checks if the addition was part of a rename.
- If a rename occurred, the path that the file used to be at is looked at for previous commits.
- This process repeats, continuing to follow renames.
Therefore, if you want to track the history of a file and want to use the pathspec to specify a file rather than just a path, use git log --follow path/to/file
. Note that this only works for one file at a time.