View the change history of a single file in Git

David Y.
jump to solution

The Problem

How can I see the change history of a single file in my Git repository?

The Solution

We can show the full change history of a file, taking renames into account, using the git log command:

git log --follow --patch -- name-of-file

The --patch/-p flag generates patch text, i.e. diffs of the file across commits. The --follow flag will include changes across renames in our change log. Note that --follow will only work if we’re looking at a single file – we should remove this flag if we’re viewing the history of multiple files, or want to exclude history past the file’s most recent renaming.

The above git log command will output our file’s history as a series of diffs. Alternatively, we can view this information in gitk, Git’s built-in repository visualizer, by entering this command:

gitk name-of-file
Pulling vs. fetching in Git
David Y.
Git "failed to push some refs to remote"
David Y.
Abort a merge in Git
David Y.
Fix "detached HEAD" in Git
David Y.

Considered "not bad" by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world's best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Sentry