Programming

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.

1 answer 5 views

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

The quickest way to see what content changed in each commit that touched a file is the patch option:

bash
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:

bash
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:

bash
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:

bash
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:

bash
git show <commit-hash>:path/to/file

You can redirect that to a file to examine it locally:

bash
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):
bash
git diff <commitA> <commitB> -- path/to/file
  • Show the change introduced by a single commit (alternative to git show):
bash
git diff <commit>^! -- path/to/file
  • See who last modified each line:
bash
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:
bash
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:
bash
git log --follow -p -- path/to/file
  • Last 5 commits that touched the file, with diffs:
bash
git log -n 5 -p -- path/to/file
  • Compact per-commit summary (diffstat):
bash
git log --follow --stat -- path/to/file
  • Inspect a single commit’s change for a file:
bash
git show 9f2a1b3 -- path/to/file
  • Recover the file content from an old commit:
bash
git show 9f2a1b3:path/to/file > old-version.txt
  • Compare how the file looked between two tags or commits:
bash
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 --follow with git log to follow history across renames; otherwise history will stop at the rename.
  • Binary files: git log -p won’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 --author to 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 -M or -C on 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

  1. https://www.atlassian.com/git/tutorials/git-log
  2. https://git-scm.com/docs/git-log
  3. https://www.atlassian.com/git/tutorials/inspecting-history/git-show
  4. 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.

Authors
Verified by moderation
Moderation
View Git File Change History with Diffs & Patches