How to list all commits that changed a specific file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with version control systems like Git, it is often necessary to view the history of changes made to a specific file. This ability is crucial for debugging issues, understanding changes over time, or collaborating effectively in team settings. Git offers powerful tools to track and list all commits that have modified a specific file. Here’s a guide on how to do that, along with explanations and examples.
Using git log
The primary tool for viewing the history of a file in Git is the git log command. This command shows the log of commits, and it can be tailored using various options to fit the needs of a specific investigation.
Basic Usage
To list all commits that have touched a specific file, use:
This command lists all the commits from the current branch that have modified the file specified in the path. Commits are shown in reverse chronological order.
Detailed Commit View
To see more detailed information about each commit, you can use:
The -p option stands for "patch" and shows the actual code changes (diffs) made in each commit affecting the file. This is very useful for seeing exactly what changes were made.
Filtering Commits
Sometimes you might want to refine the list of commits. For example, you can use:
This filters the log to only show commits made by a specific author. Another common filter is by date:
This lists commits affecting a file in a specific date range.
Advanced Git Log Searches
For more complex searches, the --grep option comes in handy:
This command lists commits that contain "Fix" in their commit messages. It can be combined with other options for more narrowed down searches.
Using git blame
Another useful tool is git blame. This command is particularly helpful if you want to see line-by-line accountability, showing which commit last modified each line of a file:
Each line of the file output will be preceded by the commit ID that last modified that line, the author, and the timestamp.
Visualization with Git GUIs
For those who prefer a graphical interface over command line, there are several GUI tools like GitKraken, SourceTree, or the Git integration in IDEs like Visual Studio Code and JetBrains IntelliJ IDEA. These tools provide visual representations of commit logs, diffs, and file histories, making it easier to digest the information.
Summary Table
Here's a summary of commands and their use cases:
| Command | Description |
git log -- path/to/file | List all commits that modified a file |
git log -p -- path/to/file | Show diffs introduced in each commit |
git log --author="Author" -- path/to/file | Filter commits by author |
git log --since="Date" -- path/to/file | Filter commits from a start date |
git log --until="Date" -- path/to/file | Filter commits until a specific date |
git log --grep="Keyword" -- path/to/file | Search commits with specific keywords in message |
git blame path/to/file | Show line-by-line last modification |
Conclusion
Understanding how to list and examine all commits that have changed a specific file is essential for effective version control and collaboration in software development. Whether through the command line or GUI tools, Git offers flexible methods to achieve detailed insights into the history of a file. Each method has its strengths and ideal use cases, and using them in combination can provide comprehensive control over your project’s version history.
Related reading
- How to list all commits that changed a specific file?
- How to list all Git tags?
- How to list all Git tags?
- How to list all tags along with the full message in git?
- How to list all tags that contain a commit?
- How to list branches that contain a given commit?
- How to list branches that contain a given commit?
- How to list only the names of files that changed between two commits
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.