Is there a way to list all commits that changed a specific file?
17 Answers
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
).
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.
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
Alternatively (since Git 1.8.4), it is also possible to just get all the commits which has changed a specific part of a file. You can get this by passing the starting line and the ending line number.
The result returned would be the list of commits that modified this particular part. The command goes like:
git log --pretty=short -u -L <upperLimit>,<lowerLimit>:<path_to_filename>
where upperLimit
is the start_line_number
and lowerLimit
is the ending_line_number
More Info - https://www.techpurohit.com/list-some-useful-git-commands
As jackrabb1t pointed out, --follow
is more robust since it continues listing the history beyond renames/moves. So, if you are looking for a file that is not currently in the same path or a file that has been renamed throughout various commits, --follow
will track it.
This can be a better option if you want to visualize the name/path changes:
git log --follow --name-status -- <path>
But if you want a more compact list with only what matters:
git log --follow --name-status --format='%H' -- <path>
or even
git log --follow --name-only --format='%H' -- <path>
The downside is that --follow
only works for a single file.
To just get a list of the commit hashes, use git rev-list
:
git rev-list HEAD <filename>
Output:
b7c4f0d7ebc3e4c61155c76b5ebc940e697600b1
e3920ac6c08a4502d1c27cea157750bd978b6443
ea62422870ea51ef21d1629420c6441927b0d3ea
4b1eb462b74c309053909ab83451e42a7239c0db
4df2b0b581e55f3d41381f035c0c2c9bd31ee98d
Which means five commits have touched this file. It's in reverse chronological order, so the first commit in the list b7c4f0d7
is the most recent one.
On Linux you can use gitk for this.
It can be installed using "sudo apt-get install git-gui gitk". It can be used to see commits of a specific file by "gitk <Filename>".