View Git File Change History with Diffs & Patches
Learn how to view complete Git file history including content changes using git log -p -- file, git show for specific commits, and git diff tools. See exact diffs, patches, and file snapshots in each commit.
How can I view the complete change history of a specific file in Git, including details of what content changed in each commit? I know that git log -- [filename] shows the commit history, but how can I see the actual file content that was modified in each commit?
Use git log -p -- <file> to view the git log file history with full patches so you can see exactly what content changed in each commit. To inspect a single commit’s change or to view the file snapshot at that commit use git show <commit> -- <file> (diff for that commit) or git show <commit>:<file> (the file contents at that commit).
Contents
- View file content changes with git log -p
- Inspect a file at a specific commit with git show
- Use git diff, blame and other tools to explore changes
- Practical examples and workflows
- Tips and common caveats
- Sources
- Conclusion
View file content changes with git log -p
The quickest way to see what content changed in each commit that touched a file is the patch option:
git log -p -- path/to/file
That shows the commit metadata (hash, author, message) followed by a full patch (diff) for each commit that modified that file. If the file was renamed at some point, add --follow to trace history across renames:
git log --follow -p -- path/to/file
Want less noise? Use --stat to get a compact summary, or combine formatting options to tailor the output:
git log --follow --stat --pretty=format:'%h %ad %an %s' -- path/to/file
For official details on git log and the -p/patch behavior see the Git documentation and a practical walkthrough on Atlassian: https://git-scm.com/docs/git-log and https://www.atlassian.com/git/tutorials/git-log.
Inspect a file at a specific commit with git show
If you already know the commit hash and want to inspect only that commit’s effect on the file, use:
git show <commit-hash> -- path/to/file
That displays the diff that commit introduced, but limited to the file you specify. If you want the full file contents as it existed in that commit (not a diff), use the blob syntax:
git show <commit-hash>:path/to/file
You can redirect that to a file to examine it locally:
git show 9f2a1b3:README.md > /tmp/README-at-9f2a1b3.md
See Atlassian’s guide for git show and community examples for these workflows: https://www.atlassian.com/git/tutorials/inspecting-history/git-show and https://stackoverflow.com/questions/1544463/how-to-get-git-log-for-a-specific-file-with-content.
Use git diff, blame and other tools to explore content changes
Sometimes you want to compare two specific versions or find who changed a line. A few commands to keep in your toolbox:
- Diff between two commits (only the file):
git diff <commitA> <commitB> -- path/to/file
- Show the change introduced by a single commit (alternative to
git show):
git diff <commit>^! -- path/to/file
- See who last modified each line:
git blame path/to/file
Want line-range blame? Use -L to limit the output to specific line numbers.
- Quick history graph for a file:
git log --oneline --graph --decorate -- path/to/file
These commands let you slice the history differently: per-commit patches, pairwise diffs, or per-line authorship. Use whichever view helps you answer the question you have right now.
Practical examples and workflows
- Show full patch history for a file including renames:
git log --follow -p -- path/to/file
- Last 5 commits that touched the file, with diffs:
git log -n 5 -p -- path/to/file
- Compact per-commit summary (diffstat):
git log --follow --stat -- path/to/file
- Inspect a single commit’s change for a file:
git show 9f2a1b3 -- path/to/file
- Recover the file content from an old commit:
git show 9f2a1b3:path/to/file > old-version.txt
- Compare how the file looked between two tags or commits:
git diff v1.2.0 v2.0.0 -- path/to/file
If you prefer a visual tool, gitk path/to/file or tig path/to/file (if installed) gives an interactive history and diffs.
Tips and common caveats
- Renames: use
--followwithgit logto follow history across renames; otherwise history will stop at the rename. - Binary files:
git log -pwon’t produce human-readable diffs for binary blobs. For binaries you’ll get metadata or a note that it’s binary. - Large output: if the file has long history, add
-n,--since, or--authorto narrow results. - Deleted files: to see a file that was deleted in a later commit, inspect the earlier commit where it still existed (e.g.,
git show <commit-before-delete>:path/to/file). - Rename/move detection tuning: Git’s rename detection can be influenced by options like
-Mor-Con commands that support them; use when rename detection seems to miss a move. The docs at https://git-scm.com/docs/git-log explain related options. - When you only need the file snapshot (not the patch), prefer
git show <commit>:<file>— it’s often cleaner for code review or extracting an old version.
Sources
- https://www.atlassian.com/git/tutorials/git-log
- https://git-scm.com/docs/git-log
- https://www.atlassian.com/git/tutorials/inspecting-history/git-show
- https://stackoverflow.com/questions/1544463/how-to-get-git-log-for-a-specific-file-with-content
Conclusion
To view a file’s complete change history including exact content changes, use git log -p -- <file> (add --follow to trace renames) and inspect individual commits with git show <commit> -- <file> or git show <commit>:<file> for the full file snapshot. Combine those with git diff and git blame for comparisons and line-level authorship, and you’ll be able to answer almost any question about how a file evolved.