How to List all Git Commits for a File |Video upload date:  · Duration: PT2M37S  · Language: EN

Quick guide to list all Git commits that touched a file with commands for tracking renames and making history readable

Want to see every commit that touched a file in Git and feel like a digital archaeologist with fewer dust mites and more command line? This quick tour shows the exact commands to list commits for a file track renames and format the output so you actually find what you came for.

Basic file scoped log

Start here if you only care about commits that changed a file directly. This shows commit ids author names dates and commit messages that include the change.

git log -- path/to/file

That output is honest and blunt. It will not follow the file through a name change by default. If the file was renamed earlier you will only see history from the current name forward.

Include renames with follow

Renames are where plain queries stop caring. Ask Git to follow the file through renames and you get continuity.

git log --follow -- path/to/file

The follow flag tells Git to walk past rename points and show commits that affected the file before it had its current name. Use this for long lived files that have been moved or renamed.

Format the output for scanning

Raw logs are fine for bedtime reading but not for quick triage. Condense or expand based on how much info you want.

git log --follow --pretty=oneline -- path/to/file

git log --follow -p -- path/to/file

The pretty option collapses each commit to one line for quick scanning. The patch option shows the actual diffs which helps when a message alone is not enough to explain what changed.

If you are hunting for a keyword pipe the output to grep like this

git log --follow --pretty=oneline -- path/to/file | grep fix

Narrow by path patterns dates or author

You can combine file path patterns with date filters and author filters to shrink the haystack.

git log --follow --since=2020-01-01 -- path/to/dir/*.js

git log --author="Alice" -- path/to/file

Use globs for directories and --since or --until to restrict by time. Quotes around names help when author names have spaces.

Inspect a specific snapshot

Once you have a commit id from the log you can inspect that commit or recover the file as it was at that point.

git show      # view commit message and diff

git checkout  -- path/to/file     # put the historical file into your working tree

git show will display the commit details and diffs. git checkout with a commit and a path retrieves that file version into your working tree so you can open it or run a quick comparison.

Quick tips

  • Use --follow plus --pretty=oneline for a compact history that spans renames
  • Add -p only when you need to see the actual diffs
  • Limit output with -n for the top N commits for quick sampling
  • Pipe to grep to hunt for keywords in messages or to find when a term first appeared
  • If a file was renamed search for its current name with --follow rather than trying to guess old names

That is all you need to stop treating file history like an archaeology dig. Use follow when names change format for quick scanning and bring in diffs only when you actually need to read the bones. Now go shine a light on that mystery commit and pretend that you always knew where the bug came from.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.