How to list all commits that changed a specific file?

GitCommit

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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 - Git

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 3 - Git

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 4 - Git

Use the command below to get commits for a specific file:

git log -p filename

Solution 5 - Git

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.

Solution 6 - Git

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

Solution 7 - Git

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.

Solution 8 - Git

If you want to look for all commits by filename and not by filepath, use:

git log --all -- '*.wmv'

Solution 9 - Git

If you are trying to --follow a file deleted in a previous commit, use

git log --follow -- filename

Solution 10 - Git

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>

Solution 11 - Git

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 imagegit commits for specific files

Solution 12 - Git

If you want to view all the commits that changed a file, in all the branches, use this:

git log --follow --all <filepath>

Solution 13 - Git

Use git log --all <filename> to view the commits influencing <filename> in all branches.

Solution 14 - Git

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.

Solution 15 - Git

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.

Solution 16 - Git

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>".

Solution 17 - Git

# 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanielView Question on Stackoverflow
Solution 1 - Gitjackrabb1tView Answer on Stackoverflow
Solution 2 - GitGabe MoothartView Answer on Stackoverflow
Solution 3 - GitBigMinerView Answer on Stackoverflow
Solution 4 - GitSankar SubburajView Answer on Stackoverflow
Solution 5 - GitrfundukView Answer on Stackoverflow
Solution 6 - GitjitendrapurohitView Answer on Stackoverflow
Solution 7 - GitRobertoView Answer on Stackoverflow
Solution 8 - GitWonderLandView Answer on Stackoverflow
Solution 9 - GitsnovelliView Answer on Stackoverflow
Solution 10 - GitCubicView Answer on Stackoverflow
Solution 11 - GitEng_FarghlyView Answer on Stackoverflow
Solution 12 - GitAlways_BeginnerView Answer on Stackoverflow
Solution 13 - GitLebnikView Answer on Stackoverflow
Solution 14 - GitBreen hoView Answer on Stackoverflow
Solution 15 - GitBoris VerkhovskiyView Answer on Stackoverflow
Solution 16 - GitChamila WijayarathnaView Answer on Stackoverflow
Solution 17 - GitAnshBikramView Answer on Stackoverflow