How to list all commits that changed a specific file?
Is there a way to list all commits that changed a 开发者_Go百科specific file?
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
).
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
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.
Use the command below to get commits for a specific file:
git log -p filename
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.
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.in/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.
If you want to look for all commits by filename
and not by filepath
, use:
git log --all -- '*.wmv'
To get all commits for a specific file use:
git rev-list HEAD --oneline FileName
For example
git rev-list HEAD --oneline index.html
Output
7a2bb2f update_index_with_alias
6c03e56 update_changes
e867142 Revert "add_paragraph"
See gif image
If you wish to see all changes made in commits that changed a particular file (rather than just the changes to the file itself), you can pass --full-diff
:
git log -p --full-diff [branch] -- <path>
If you are trying to --follow a file deleted in a previous commit, use
git log --follow -- filename
If you want to view all the commits that changed a file, in all the branches, use this:
git log --follow --all <filepath>
Use git log --all <filename>
to view the commits influencing <filename>
in all branches.
gitk <path_to_filename>
Assuming the package "gitk" is already installed.
If it is not installed, do this:
sudo apt-get install gitk
And then try the above command. It is for Linux... It might help Linux users if they want a GUI.
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.
# Shows commit history with patch
git log -p -<no_of_commits> --follow <file_name>
# Shows brief details like "1 file changed, 6 insertions(+), 1 deletion(-)"
git log --stat --follow <file_name>
Reference
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>".
精彩评论