How to list all commits that changed a specific file?
Solution 1
The --follow
works for a particular file
git log --follow -- filename
Difference to other solutions given
Note that other solutions include git log path
(without the --follow
). That approach is handy if you want to track e.g. changes in a directory, but stumbles when files were renamed (thus use --follow filename
).
Solution 2
I have been looking at this closely and all these answers don‘t seem to really show me all the commits across all the branches.
Here is what I have come up with by messing around with the gitk edit view options. This shows me all the commits for a file regardless of branch, local, reflog, and remote.
gitk --all --first-parent --remotes --reflog --author-date-order -- filename
It also works with git log
:
git log --all --first-parent --remotes --reflog --author-date-order -- filename
Solution 3
git log path
should do what you want. From the git log
man page:
[--] <path>…
Show only commits that affect any of the specified paths. To prevent confusion with
options and branch names, paths may need to be prefixed with "-- " to separate them
from options or refnames.
Solution 4
Use the command below to get commits for a specific file:
git log -p filename
Solution 5
It should be as simple as git log <somepath>
; check the manpage (git-log(1)
).
Personally I like to use git log --stat <path>
so I can see the impact of each commit on the file.
Daniel
Updated on July 13, 2022Comments
-
Daniel almost 2 years
Is there a way to list all commits that changed a specific file?
-
benhorgen over 4 yearsAre you looking for changes to a file across a single branch; across all local branches; across all branches on a single remote; or across it all? I think across it all will require a script.
-
-
Cascabel almost 14 yearsOr even
-p
if you want to see the full diff, not just that it had some number of lines modified. -
rfunduk almost 14 yearsTrue, but that's pretty noisy considering most files have been changed many times over their lives. I don't want to see full diffs of every single commit that ever touched a file. I'm usually looking for a specific thing, so I can get a log with just impacts and then
git show
on the specific commits that look like they matter. -
Yo Ludke about 11 yearsThis is better, it seems to list the commits concerning the file across all commits unlike just ` git log <somepath>`
-
Ibrahim Quraish almost 11 years+1
--follow
accounts for renames, so this is more robust thangit log -- path
-
kwahn about 10 yearsDoes not work if the file's path has changed. jackrabbit's answer does work for this case.
-
StvnW over 9 years
--follow
works for a single path, which could be a directory. If passed a directory it will run recursively and report changes to all files below that point. -
StvnW over 9 yearsNote that
--follow
accepts a path, which can be a file but also a directory. In the case of the latter it will run recursively and report changes to all files below that point. (This behaviour is not documented in the manpage and may not be permanent.) -
VaTo about 9 yearsWhats the difference between that one and just
git log filename?
-
MarSoft about 9 years@SaulOrtega,
git log filename
doesn't follow file renaming, i.e. it will show all commits regarding that filename (not actual file). If you create filesX
andY
, changed both, then deletedY
and renamedX
toY
and then also changed it, and you rungit log Y
, you will get messages for both oldY
and new one. And the opposite, with--follow
you will get commits regarding that file when it was namedX
and when it was namedY
. -
Lebnik almost 9 yearsuse "git log --all filename" for view all commits in all branches
-
zkent over 8 yearsI understand that this doesn't exactly answer the question since he wanted a list of commits but this is gold and going in my file.
-
earthmeLon over 8 yearsThis is perfect for when someone makes changes but they forget where they commited the changes.
-
Tino about 8 yearsFor
git
newbies: Usegit log -p --follow -- filename
to display the changes as well. Also note: "filename" can be a file, a directory or a submodule. -
Aaron Swan over 7 yearsThis works if you need to restrict the log to a specific branch
-
Juan Antonio Tubío about 7 yearsVery useful. Includes also stash commits
-
code over 6 yearsAny way to do this using the GitHub API for a particular file?
-
reducing activity almost 6 years@AbhishekSoni That seems to be a new question.
-
Anentropic almost 6 yearsor without the
[branch]
-
Cubic almost 6 years@Anentropic The square brackets were supposed to indicate that the argument is optional.
-
ThanhLD over 5 yearsIt's all that I need, It shows full change, includes some change from the merge.
-
borneo777 over 5 yearsDoes this command (git log --follow -- filename) view all commits in all branches?
-
Sergio Belevskij over 5 yearsgit log --stat --follow -- *.html => output list of commits with exactly one files in each commit. Very nice!
-
Danilo Souza Morães over 5 years-- is used in several unix commands to indicate file names. Since --follow is a valid file name in unix, if you wanted to show the logs for that file and types
git log --follow
it would be interpreted as the --follow option, not the file, so you would do:git log -- --follow
-
Sławomir Lenart about 5 yearsuse with
-p
to not only list but preview the followed changes. -
carl verbiest almost 5 yearsThis should be the prefered answer. The question was to find all commits, this one does.
-
Soren Bjornstad almost 5 yearsNotice that
--reflog
includes commits which were rebased/amended or otherwise discarded. Maybe that should be obvious, but I tried to use this withgit log
and was trying to figure out why I was seeing seemingly duplicated commits! -
Soren Bjornstad almost 5 yearsThis won't work at all if the file doesn't exist in the currently checked-out branch. You can add the branch like
git log -p mybranch -- filename
or just usegit log --all -- filename
to look in all branches. -
Soren Bjornstad over 4 yearsFor Windows users, note that
gitk
is bundled with Git for Windows. -
Hilikus about 4 yearsthe last argument is not really the filename. it should be the full path (absolute or relative) to the file. Just using the filename doesn't return anything since git can't find the file. i.e.
-- src/main/java/hello.java
, not-- hello.java
-
Petr Bodnár over 3 yearsAgreed with @Hilikus. For example, if you want to search for
hello.java
changes anywhere in the tree, you may use-- **/hello.java
. Git documentation seems to be utterly underdocumented on this. -
Ewan over 3 yearsI don't get it..the accepted answer by @gabe-moothart shows all parent commits of the commit that creates a file on my test tree - ie commits that don't modify the file. This answer works perfectly on my test repo though.
-
Peter Mortensen about 3 years
-
K. Symbol about 3 yearsHowever, this only shows the changes for this file in each commit, how to let gitk show all changes (including this file and other files that co-changes)?
-
destroyer22719 almost 3 yearsI like your answer better because
--oneline
works whereas-p
doesn't work for that flag. -
mz0 almost 3 yearsthis is a life-saver (should I say "file-saver")) when branch was forcefully rewritten
-
Niko O over 2 yearsFor posterity: In my version of git (
2.33.0.windows.2.7.g2aa9e28d2a.20210908104152
) I have to not use the double-dash before the file path, even when looking for a file and not a directory. -
Olivier over 2 years@NikoO: In my version (2.34.1), using that double-dash seems totally optional, but works well if typed.
-
Moberg about 2 yearsTHANK YOU, this needs more upvotes.
git log --full-diff --stat -- <path>
was the command I was looking for. :)