Categories
git git-log

View the change history of a file using Git versioning

3571

How do I view the history of an individual file with complete details of what has changed?

git log -- [filename] shows me the commit history of a file, but how do I see the file content that changed?

2

  • 5

    The link above (posted by Chris) is no longer valid. This link is working today: git-scm.com/book/en/v2

    – Cog

    Apr 23, 2019 at 22:10


  • 1

    @chris: What is the link you speak of? In some now-deleted comment?

    Jul 6, 2021 at 8:32

2646

For a graphical view, use gitk:

gitk [filename]

To follow the file across file renames:

gitk --follow [filename]

25

  • 36

    But I rather even have a tool that combined the above with ‘git blame’ allowing me to browse the source of a file as it changes in time…

    Apr 6, 2010 at 15:50

  • 29

    Unfortunately, this doesn’t follow the history of the file past renames.

    Mar 30, 2011 at 23:17

  • 154

    I was also looking for the history of files that were previously renamed and found this thread first. The solution is to use “git log –follow <filename>” as Phil pointed out here.

    Apr 26, 2011 at 9:05

  • 129

    The author was looking for a command line tool. While gitk comes with GIT, it’s neither a command line app nor a particularly good GUI.

    Jul 18, 2011 at 15:17

  • 79

    Was he looking for a command line tool? “right click -> show history” certainly doesn’t imply it.

    – hdgarrood

    May 13, 2013 at 14:57

2646

For a graphical view, use gitk:

gitk [filename]

To follow the file across file renames:

gitk --follow [filename]

25

  • 36

    But I rather even have a tool that combined the above with ‘git blame’ allowing me to browse the source of a file as it changes in time…

    Apr 6, 2010 at 15:50

  • 29

    Unfortunately, this doesn’t follow the history of the file past renames.

    Mar 30, 2011 at 23:17

  • 154

    I was also looking for the history of files that were previously renamed and found this thread first. The solution is to use “git log –follow <filename>” as Phil pointed out here.

    Apr 26, 2011 at 9:05

  • 129

    The author was looking for a command line tool. While gitk comes with GIT, it’s neither a command line app nor a particularly good GUI.

    Jul 18, 2011 at 15:17

  • 79

    Was he looking for a command line tool? “right click -> show history” certainly doesn’t imply it.

    – hdgarrood

    May 13, 2013 at 14:57

1712

git log --follow -p -- path-to-file

This will show the entire history of the file (including history beyond renames and with diffs for each change).

In other words, if the file named bar was once named foo, then git log -p bar (without the --follow option) will only show the file’s history up to the point where it was renamed — it won’t show the file’s history when it was known as foo. Using git log --follow -p bar will show the file’s entire history, including any changes to the file when it was known as foo. The -p option ensures that diffs are included for each change.

13

  • 23

    –stat is also helpful. You can use it together with -p.

    May 9, 2012 at 22:29

  • 33

    I agree this is the REAL answer. (1.) --follow ensures that you see file renames (2.) -p ensures that you see how the file gets changed (3.) it is command line only.

    Sep 11, 2012 at 18:54

  • 4

    @NHDaly I notice that the -- was added, but I don’t know why this makes it best? What is it that it does?

    – Benjohn

    May 27, 2015 at 15:10

  • 50

    @Benjohn The -- option tells Git that it has reached the end of the options and that anything that follows -- should be treated as an argument. For git log this only makes any difference if you have a path name that begins with a dash. Say you wanted to know the history of a file that has the unfortunate name “–follow”: git log --follow -p -- --follow

    May 28, 2015 at 16:10

  • 10

    @Benjohn: Normally, the -- is useful because it can also guard against any revision names that match the filename you’ve entered, which can actually be scary. For example: If you had both a branch and a file named foo, git log -p foo would show the git log history up to foo, not the history for the file foo. But @DanMoulding is right that since the --follow command only takes a single filename as its argument, this is less necessary since it can’t be a revision. I just learned that. Maybe you were right to leave it out of your answer then; I’m not sure.

    – NHDaly

    May 30, 2015 at 6:03