-C (copy/rename detection) and -M (rename detection) flags with the git log command. When a file is renamed, Git treats it as a deletion followed by an addition unless explicitly told otherwise. By enabling these options, Git attempts to detect copies and renames based on content similarity, allowing you to see the full lineage of a file even after its path has changed in the repository.
Understanding How Git Tracks Renames
By default, Git does not track file renames because they are technically two separate operations: deleting the old file and creating a new one. The version control system relies on content hashing to identify objects. If you rename a file without any changes to its content, Git sees two distinct blobs of data if you do not use specific flags. This behavior can make it difficult to trace the history of a file that has been reorganized or moved within the project structure. To overcome this, Git provides built-in heuristics to guess whether a deletion and an addition are actually related. The primary tool for this investigation is thegit log command. While standard logs show commit messages and file paths, adding specific parameters instructs Git to analyze the diff between commits for structural changes. This is particularly useful when auditing codebases where files have been refactored or moved into different directories over time. Without these flags, your history might appear fragmented, showing a sudden disappearance of a file and the appearance of a new one with no apparent connection.
Using Flags to Detect Moved Files
To see the history of a file that was moved, you need to use the-M flag for rename detection. This flag tells Git to look for files that were deleted and added back with similar content. You can combine this with the -C flag to detect copies as well. These flags work best when applied to the git log command along with the --follow option, which keeps following the file across renames.
Here is the basic syntax to view the history of a current file while detecting renames:
git log --follow -M -C -- path/to/current/file.txt
In this command, --follow ensures that Git continues tracing the file's history even after it has been renamed. The -M flag enables rename detection, and -C enables copy detection. If you only want to detect renames and not copies, you can omit the -C flag. The path specified should be the current name of the file. Git will then output all commits that affected this file, including those where it existed under a different name.
You can also adjust the sensitivity of the detection using the -M flag with a percentage threshold. For example, -M80% requires at least 80% similarity between the deleted and added content to consider them a rename. This is helpful in large projects where minor changes might obscure the true nature of a file movement.
Recovering Deleted File History
When a file is completely deleted from the working directory and committed, it still exists in the Git object database until garbage collection runs. You can view the history of such a file by searching through the repository's reflog or by usinggit log with specific filters. However, if the file is gone from the current branch, you must specify the exact previous path or search for it by content.
One effective method is to use git log --diff-filter=D to list all deleted files in the history. This shows you every commit where a file was removed. Once you identify the relevant commit, you can inspect the details of that commit to find the original filename.
git log --diff-filter=D --summary
This command outputs a summary of each commit that involved a deletion. The --summary flag provides concise information about the file operations, including the mode changes and the file path. This is crucial for identifying which file was deleted and when. After finding the commit hash, you can use git show to view the full diff and understand what was removed.
If you know the approximate name of the deleted file, you can use git log --all --full-history -- *pattern* to search across all branches. This helps locate files that may have been deleted in a feature branch that was later merged and cleaned up.
Integrating with Workflow Tools
For teams managing complex repositories, integrating rename detection into daily workflows prevents confusion during code reviews. Using aliases for common commands can streamline the process. For instance, creating a shell alias forgit log --follow -M saves time when investigating file movements. This approach ensures consistency across the team and reduces the cognitive load when navigating historical changes.
Additionally, understanding how Git handles these operations helps in choosing the right collaboration strategy. If your team frequently moves files around, enforcing consistent naming conventions and documenting major refactorings can reduce the need for deep historical dives. However, having the technical capability to trace these changes remains essential for debugging issues related to missing configurations or lost assets.
Related articles on the same site:
Mastering Git Rebase: The -r Flag and Beyond
Git: Why 'git checkout --force' Doesn't Always Revert Modified Changes?
Frequently Asked Questions
Why doesn't git log show my renamed file?
By default, Git treats renames as separate delete and add operations. It does not link them automatically in the log output. You must use the -M flag to enable rename detection, which analyzes content similarity to connect the old and new file names.
Can I detect renames across different branches?
Yes, but you must specify the branch name or use --all to search all branches. Standard git log only looks at the current branch history. Combining --all with -M and --follow allows you to trace a file's journey across multiple development lines.
What is the difference between -M and -C flags?
The -M flag detects renames, meaning a file was moved from one location to another. The -C flag detects copies, meaning a file was duplicated to a new location while the original remained. Using both flags together covers most scenarios involving file duplication and movement.
How do I find a file that was deleted weeks ago?
Use git log --diff-filter=D --summary to list all deletions. This command highlights every commit where a file was removed. You can then filter the results by date or filename pattern to locate the specific deletion event and recover the history.
Does git blame follow renames automatically?
No, git blame does not follow renames by default. It attributes lines to the commit where they last appeared in the current file. To get blame information across renames, you must use the -C or -M flags with git blame, similar to how you use them with git log.