Daniel
Daniel

Reputation: 12245

How to list all commits that changed a specific file?

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

Upvotes: 1191

Views: 481589

Answers (18)

cng.buff
cng.buff

Reputation: 575

If you use VSCODE, you can use Git History Extension or access TIMELINE as shown below.

enter image description here

Upvotes: 1

jitendrapurohit
jitendrapurohit

Reputation: 9675

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

Upvotes: 29

Eng_Farghly
Eng_Farghly

Reputation: 2997

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

Upvotes: 20

user3064538
user3064538

Reputation:

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.

Upvotes: 4

BigMiner
BigMiner

Reputation: 2192

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

Upvotes: 177

snovelli
snovelli

Reputation: 6058

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

git log --follow -- filename

Upvotes: 11

Chamila Wijayarathna
Chamila Wijayarathna

Reputation: 1933

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

Upvotes: 1

Roberto
Roberto

Reputation: 11933

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.

Upvotes: 19

Gabe Moothart
Gabe Moothart

Reputation: 32072

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.

Upvotes: 133

jackrabb1t
jackrabb1t

Reputation: 15729

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

Upvotes: 1542

WonderLand
WonderLand

Reputation: 5664

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

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

Upvotes: 14

AnshBikram
AnshBikram

Reputation: 2148

# 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

Upvotes: 1

Always_Beginner
Always_Beginner

Reputation: 2886

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

git log --follow --all <filepath>

Upvotes: 9

Breen ho
Breen ho

Reputation: 1631

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.

Upvotes: 4

rfunduk
rfunduk

Reputation: 30432

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.

Upvotes: 44

Cubic
Cubic

Reputation: 15673

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>

Upvotes: 14

Sankar Subburaj
Sankar Subburaj

Reputation: 5042

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

git log -p filename

Upvotes: 54

Lebnik
Lebnik

Reputation: 636

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

Upvotes: 8

Related Questions