159

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.

10 Answers 10

183

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
1
  • 10
    Thanks for the -p tip, that's super useful for finding which revisions involved a bit of code. Mar 20, 2014 at 21:26
116

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
3
  • 4
    Here's another nod for gitk, which provides a great way to browse all snapshots of a single file in a git repo.
    – Ray Brown
    Apr 25, 2011 at 20:33
  • 1
    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.)
    – antinome
    Jul 18, 2014 at 22:47
  • Does gitk work on Windows? If so, what environment is required? Oct 4, 2021 at 20:24
53

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.

39

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.

35

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

tig path/to/file
1
  • Should be the accepted answer!
    – ZeroCool
    May 26, 2022 at 18:51
11

Many Git history browsers, including git log (and 'git log --graph'), gitk (in Tcl/Tk, part of Git), QGit (in Qt), tig (text mode interface to Git, using ncurses), Giggle (in GTK+), TortoiseGit and git-cheetah support path limiting (e.g., gitk path/to/file).

1
  • @RobertVuković I know this is an old question... but see my answer below!
    – brianc
    Jan 23, 2015 at 18:28
7

git log --all -- path/to/file should work

5
  • There's no link in my answer @ineersa Dec 3, 2015 at 14:33
  • 3
    This should be the top answer. I dislike using crappy gui tools
    – Grobi
    Apr 7, 2020 at 8:56
  • 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.) Sep 17, 2021 at 20:01
  • @PeterMortensen Check the documentation here: git-scm.com/docs/git-log#Documentation/git-log.txt---all Sep 19, 2021 at 12:04
  • @EdsonMedina Instead of putting the link to documentation in the comment, it would be better to put it into the answer and copy the relevant parts of it here. "RTFM" (even without the F) sounds arrogant. Jun 28, 2023 at 10:34
4

Of course, if you want something as close to TortoiseSVN as possible, you could just use TortoiseGit.

1
  • 1
    Except that the TortoiseSvn shell extensions contain a command for showing the history of a single file, whereas TortoiseGit does not.
    – Neutrino
    Jan 21, 2020 at 15:51
3

TortoiseGit also provides a command line tool to do see the history of a file. Using PowerShell:

C:\Program` Files\TortoiseGit\bin\TortoiseGitProc.exe /command:log /path:"c:\path\to\your\file.txt"
2

You can use git-diff or git-log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.