git diff --follow is a Git command that is typically used to view the change history of a file, including across renames. Let’s break down the command into its components to understand it better.
git diff: This command is used in Git to show changes between commits, commit and working tree, etc. By default, it shows the changes in the code that you have not yet staged (added to the Git index) for the next commit.
--follow: This option is used in conjunction with
git diffcommand to track the history of a file beyond renames. It instructs Git to list the history of a file beyond renames, useful when a file has been renamed and you wish to trace its history.
However, as of my knowledge cutoff in September 2021, there’s a caveat that
--follow option is not compatible with
git diff command, rather it’s commonly used with
git log -p to show the history of changes to a file, including renames.
You can use
git log --follow -p -- <file> to achieve the objective.
Here’s a sample usage:
git log --follow -p -- myfile.txt
This command will show the entire commit history of
myfile.txt, including the changes made in each commit (that’s what
-p is for), and it will continue listing the history even across renames due to the
If the file has been renamed at some point in its history, the log will include its previous names and the changes made under those names as well. This is useful when you’re trying to understand the evolution of a file in a project over time, particularly in projects where files are often renamed or moved around.
Do note that the
--follow option only works with a single file at a time. You can’t use it to track multiple files at once.
It’s always good to refer to the official Git documentation or use the
git help command for the most accurate and up-to-date information, as Git is under active development and its features and behavior may change over time.