How can I find the commit in which a given file was added?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Git can show you the commit where a file first appeared, but the exact command depends on whether the file kept the same path the whole time. The simplest case is a normal added file at the current path. Renames and copies make the question slightly more subtle.
Use git log --diff-filter=A
For the most direct case, run:
--diff-filter=A restricts the log to commits where the file was added. If you want only the first matching commit in a compact form:
Or more explicitly, ask for just one result in reverse chronological order by using --reverse:
That usually gives the commit where the file first entered the repository at that path.
Understand what this command means
Git does not store "creation date of file" as a dedicated field. Instead, it compares snapshots between commits. The A filter means "show commits where this path was added relative to its parent."
That is why git log is the right tool here. You are not querying metadata attached to the file itself; you are walking commit history and asking where that path first showed up as an addition.
Watch out for renames
If the file was renamed later, the current path may not have been the original path. In that case, a simple git log --diff-filter=A -- current/path may only show the rename-era addition of the current name rather than the true origin of the content.
Start with rename-aware history:
--follow tells Git to continue walking history across renames for a single file path. Once you know the historical path chain, you can inspect the earliest commit in that followed history:
This is often the better real-world answer when a file has been renamed.
A more detailed inspection example
If you want to see exactly how Git described the file in that commit:
This can confirm whether the file was:
- added
- renamed
- copied
That extra confirmation is useful in repositories with lots of history rewriting or file movement.
Use branch context carefully
The answer depends on which history you are walking. If the file was introduced on a different branch that is not merged into your current branch, your current git log view will not see it.
If you need to search all reachable references:
And if you are trying to answer "When did this file enter the main branch?" run the command while explicitly comparing or inspecting the mainline history rather than every ref in the repository.
When the file was copied, not added from scratch
Git can also detect copies in some contexts, but "first appearance of content" and "first appearance of this path" are different questions.
If you ask for the commit where the path was added, the commands above are correct.
If you ask for the origin of the file's content before a rename or copy, the investigation becomes more heuristic because Git infers similarity rather than storing an absolute "came from here" pointer for all cases.
Common Pitfalls
The biggest mistake is forgetting the -- separator before the file path. Without it, Git can misread the path as a revision argument.
Another common issue is ignoring renames. A file may appear to have been "added" recently only because the current path was created by a rename.
People also forget that git log answers questions within the history you asked it to traverse. If the branch context is wrong, the result will be incomplete.
Finally, do not confuse "first commit that touched this current path" with "first historical origin of the content." Those are related but different questions.
Summary
- Start with
git log --diff-filter=A -- path/to/filefor the simple case. - Add
--reversewhen you want the earliest matching commit first. - Use
--followwhen the file may have been renamed. - Use
git show --summaryto confirm how Git classified the path in the candidate commit. - Be clear whether you want the first appearance of the path or the deeper origin of the content.

