How can I view the history of changes to one file in Git?
Quick answer
From your repository, run git log -- path/to/file to list commits affecting that path. Add -p to display patches for those commits: git log -p -- path/to/file. If the file may have been renamed, try git log -p --follow -- path/to/file. --follow applies to one file, and path changes or rename detection can affect which earlier commits appear. [S1]
The question, the details, the next step.
A practical checklist
Run the command in the repository whose history you want to inspect.
Put the file path after
--to limit the log.Add
-pto display the changes for listed commits.Try
--followif a single file may have been renamed.If expected history is missing, consider whether the file had an earlier path.
A few more useful answers
What does `--` do in `git log -- path/to/file`?
It marks the path limitation after the log options, so the command narrows the history to commits affecting that path. [S1]
Does `--follow` guarantee that I’ll see every version of a file?
No. It can continue a single file’s history beyond renames, but path changes and rename detection can affect what appears. [S1]
Sources
References used for this guide.
- [S1] Git - git-log Documentation git-scm.com · Checked
