156

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mrblah
  • 99,669
  • 140
  • 310
  • 420

10 Answers10

180

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
Christian
  • 9,914
  • 6
  • 45
  • 52
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
baudtack
  • 29,062
  • 9
  • 53
  • 61
  • 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 '11 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 '14 at 22:47
  • Does gitk work on Windows? If so, what environment is required? – Peter Mortensen Oct 04 '21 at 20:24
51

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.

chopper
  • 6,649
  • 7
  • 36
  • 53
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.

Gustavo Litovsky
  • 2,457
  • 1
  • 29
  • 41
35

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

tig path/to/file
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user217433
  • 459
  • 3
  • 2
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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
6

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

Edson Medina
  • 9,862
  • 3
  • 40
  • 51
  • There's no link in my answer @ineersa – Edson Medina Dec 03 '15 at 14:33
  • 3
    This should be the top answer. I dislike using crappy gui tools – Grobi Apr 07 '20 at 08: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.) – Peter Mortensen Sep 17 '21 at 20:01
  • @PeterMortensen Check the documentation here: https://git-scm.com/docs/git-log#Documentation/git-log.txt---all – Edson Medina Sep 19 '21 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. – Honza Zidek Jun 28 '23 at 10:34
4

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

Community
  • 1
  • 1
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 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 '20 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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brianc
  • 1,547
  • 3
  • 16
  • 30
2

You can use git-diff or git-log.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vladikoff
  • 1,496
  • 1
  • 14
  • 25