How to list all commits that changed a specific file?

371,549

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.

Share:
371,549
Daniel
Author by

Daniel

Updated on July 13, 2022

Comments

  • Daniel
    Daniel almost 2 years

    Is there a way to list all commits that changed a specific file?

    • benhorgen
      benhorgen over 4 years
      Are 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
    Cascabel almost 14 years
    Or even -p if you want to see the full diff, not just that it had some number of lines modified.
  • rfunduk
    rfunduk almost 14 years
    True, 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
    Yo Ludke about 11 years
    This is better, it seems to list the commits concerning the file across all commits unlike just ` git log <somepath>`
  • Ibrahim Quraish
    Ibrahim Quraish almost 11 years
    +1 --follow accounts for renames, so this is more robust than git log -- path
  • kwahn
    kwahn about 10 years
    Does not work if the file's path has changed. jackrabbit's answer does work for this case.
  • StvnW
    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
    StvnW over 9 years
    Note 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
    VaTo about 9 years
    Whats the difference between that one and just git log filename?
  • MarSoft
    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 files X and Y, changed both, then deleted Y and renamed X to Y and then also changed it, and you run git log Y, you will get messages for both old Y and new one. And the opposite, with --follow you will get commits regarding that file when it was named X and when it was named Y.
  • Lebnik
    Lebnik almost 9 years
    use "git log --all filename" for view all commits in all branches
  • zkent
    zkent over 8 years
    I 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
    earthmeLon over 8 years
    This is perfect for when someone makes changes but they forget where they commited the changes.
  • Tino
    Tino about 8 years
    For git newbies: Use git log -p --follow -- filename to display the changes as well. Also note: "filename" can be a file, a directory or a submodule.
  • Aaron Swan
    Aaron Swan over 7 years
    This works if you need to restrict the log to a specific branch
  • Juan Antonio Tubío
    Juan Antonio Tubío about 7 years
    Very useful. Includes also stash commits
  • code
    code over 6 years
    Any way to do this using the GitHub API for a particular file?
  • reducing activity
    reducing activity almost 6 years
    @AbhishekSoni That seems to be a new question.
  • Anentropic
    Anentropic almost 6 years
    or without the [branch]
  • Cubic
    Cubic almost 6 years
    @Anentropic The square brackets were supposed to indicate that the argument is optional.
  • ThanhLD
    ThanhLD over 5 years
    It's all that I need, It shows full change, includes some change from the merge.
  • borneo777
    borneo777 over 5 years
    Does this command (git log --follow -- filename) view all commits in all branches?
  • Sergio Belevskij
    Sergio Belevskij over 5 years
    git log --stat --follow -- *.html => output list of commits with exactly one files in each commit. Very nice!
  • Danilo Souza Morães
    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
    Sławomir Lenart about 5 years
    use with -p to not only list but preview the followed changes.
  • carl verbiest
    carl verbiest almost 5 years
    This should be the prefered answer. The question was to find all commits, this one does.
  • Soren Bjornstad
    Soren Bjornstad almost 5 years
    Notice that --reflog includes commits which were rebased/amended or otherwise discarded. Maybe that should be obvious, but I tried to use this with git log and was trying to figure out why I was seeing seemingly duplicated commits!
  • Soren Bjornstad
    Soren Bjornstad almost 5 years
    This 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 use git log --all -- filename to look in all branches.
  • Soren Bjornstad
    Soren Bjornstad over 4 years
    For Windows users, note that gitk is bundled with Git for Windows.
  • Hilikus
    Hilikus about 4 years
    the 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
    Petr Bodnár over 3 years
    Agreed 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
    Ewan over 3 years
    I 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
    Peter Mortensen about 3 years
    The link is broken: HTTP: "techpurohit.com has expired and is parked free". HTTPS: "An error occurred during a connection to www.techpurohit.com. PR_END_OF_FILE_ERROR"
  • K. Symbol
    K. Symbol about 3 years
    However, 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
    destroyer22719 almost 3 years
    I like your answer better because --oneline works whereas -p doesn't work for that flag.
  • mz0
    mz0 almost 3 years
    this is a life-saver (should I say "file-saver")) when branch was forcefully rewritten
  • Niko O
    Niko O over 2 years
    For 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
    Olivier over 2 years
    @NikoO: In my version (2.34.1), using that double-dash seems totally optional, but works well if typed.
  • Moberg
    Moberg about 2 years
    THANK YOU, this needs more upvotes. git log --full-diff --stat -- <path> was the command I was looking for. :)