See changes to a specific file using git

Learn see changes to a specific file using git with practical examples, diagrams, and best practices. Covers git, git-svn development techniques with visual explanations.

Mastering Git: How to View Changes to a Specific File

Mastering Git: How to View Changes to a Specific File

Learn various Git commands to inspect the modification history of a single file, from recent changes to its entire lifecycle. This article covers git diff, git log, and git blame with practical examples.

Understanding the evolution of a single file within a Git repository is a fundamental skill for any developer. Whether you're debugging an issue, reviewing code, or simply curious about who changed what and when, Git provides powerful tools to drill down into file-specific modifications. This guide will walk you through the most effective commands to achieve this, offering insights into each command's capabilities and common use cases.

Inspecting Recent Changes with git diff

The git diff command is your primary tool for seeing uncommitted changes or differences between various commits, branches, or the working directory and the index. When you want to see what changes have been made to a specific file since the last commit, or between two different points in history, git diff is invaluable.

git diff <file-path>

Show changes in a file in your working directory not yet staged.

git diff --staged <file-path>

Show changes in a file that are staged but not yet committed.

git diff <commit1> <commit2> <file-path>

Compare a file between two specific commits.

Tracing File History with git log

While git diff shows the actual changes, git log provides a powerful way to view the commit history affecting a specific file. This allows you to see every commit where the file was modified, along with the commit message, author, and timestamp. It's excellent for understanding the chronological development of a file.

git log <file-path>

View all commits that modified the specified file.

git log -p <file-path>

Show commit messages and the actual diff for each change to the file.

git log --oneline --follow <file-path>

A concise log, useful for renamed files (--follow).

A flowchart diagram illustrating the Git log process for a specific file. It starts with 'User requests git log ', branches to 'File exists in commit?', then 'Show commit details (hash, author, date, message)' and 'Show diff if -p is used'. The flow continues until 'All relevant commits processed?'. Use blue rounded rectangles for processes, green diamonds for decisions, and black arrows for flow.

Workflow for git log <file>

Pinpointing Authorship with git blame

Sometimes you need to know not just what changed, but who changed a specific line and when. The git blame command answers this by showing the author, commit hash, and timestamp for each line in a file. This is particularly useful for understanding the context of a specific line of code or for identifying who to talk to about a particular change.

git blame <file-path>

Show line-by-line authorship information for a file.

git blame -L <start>,<end> <file-path>

Blame only a specific range of lines in the file.

By combining these commands, you can gain a complete understanding of a file's history within your Git repository. From quick diffs to detailed logs and line-by-line authorship, Git provides all the tools you need to explore and understand your codebase effectively.