How can I view file history in Git?

186,110

Solution 1

Use git log to view the commit history. Each commit has an associated revision specifier that is a hash key (e.g. 14b8d0982044b0c49f7a855e396206ee65c0e787 and b410ad4619d296f9d37f0db3d0ff5b9066838b39). To view the difference between two different commits, use git diff with the first few characters of the revision specifiers of both commits, like so:

# diff between commits 14b8... and b410...
git diff 14b8..b410
# only include diff of specified files
git diff 14b8..b410 path/to/file/a path/to/file/b

If you want to get an overview over all the differences that happened from commit to commit, use git log or git whatchanged with the patch option:

# include patch displays in the commit history
git log -p
git whatchanged -p
# only get history of those commits that touch specified paths
git log path/a path/b
git whatchanged path/c path/d

Solution 2

It looks like you want git diff and/or git log. Also check out gitk:

gitk path/to/file
git diff path/to/file
git log path/to/file

Solution 3

My favorite is git log -p <filename>, which will give you a history of all the commits of the given file as well as the diffs for each commit.

Solution 4

I like to use gitk name_of_file

This shows a nice list of the changes that happened to a file at each commit, instead of showing the changes to all the files. Makes it easier to track down something that happened.

Solution 5

You could also use tig for a nice, ncurses-based Git repository browser. To view history of a file:

tig path/to/file
Share:
186,110
mrblah
Author by

mrblah

test testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest test asdf asdf

Updated on October 05, 2021

Comments

  • mrblah
    mrblah over 2 years

    With Subversion I could use TortoiseSVN to view the history/log of a file.

    How can I do this with Git?

    I am just looking for the history record for a particular file, and then the ability to compare the different versions.

  • Ray Brown
    Ray Brown about 13 years
    Here's another nod for gitk, which provides a great way to browse all snapshots of a single file in a git repo.
  • Christopher Pickslay
    Christopher Pickslay about 10 years
    Thanks for the -p tip, that's super useful for finding which revisions involved a bit of code.
  • antinome
    antinome almost 10 years
    By default gitk shows the diff plus 10 lines of context, but what if you want to see a snapshot of the whole file? Simply set "Lines of context" to a large value (e.g. 100000). Then you can flip back and forth between commits and see the entire file at different points in time. (You can also search within the file.)
  • brianc
    brianc over 9 years
    @RobertVuković I know this is an old question... but see my answer below!
  • Edson Medina
    Edson Medina over 8 years
    There's no link in my answer @ineersa
  • Neutrino
    Neutrino over 4 years
    Except that the TortoiseSvn shell extensions contain a command for showing the history of a single file, whereas TortoiseGit does not.
  • Grobi
    Grobi about 4 years
    This should be the top answer. I dislike using crappy gui tools
  • Peter Mortensen
    Peter Mortensen over 2 years
    Can you elaborate a little bit in your answer? E.g., why is option --all required? What is it supposed to do? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)
  • Edson Medina
    Edson Medina over 2 years
    @PeterMortensen Check the documentation here: git-scm.com/docs/git-log#Documentation/git-log.txt---all
  • Peter Mortensen
    Peter Mortensen over 2 years
    Does gitk work on Windows? If so, what environment is required?
  • ZeroCool
    ZeroCool almost 2 years
    Should be the accepted answer!