How can I view file history in Git?
Asked Answered
L

10

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.

Lancer answered 23/11, 2009 at 21:17 Comment(0)
E
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
Eliath answered 23/11, 2009 at 21:39 Comment(1)
Thanks for the -p tip, that's super useful for finding which revisions involved a bit of code.Tequilater
A
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
Aster answered 23/11, 2009 at 21:20 Comment(3)
Here's another nod for gitk, which provides a great way to browse all snapshots of a single file in a git repo.Diocese
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.)Volans
Does gitk work on Windows? If so, what environment is required?Scraper
C
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.

Cerement answered 19/5, 2015 at 4:56 Comment(0)
C
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.

Cy answered 27/8, 2012 at 21:30 Comment(0)
S
35

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

tig path/to/file
Schmaltzy answered 23/11, 2009 at 23:19 Comment(1)
Should be the accepted answer!Cornerwise
T
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).

Terminator answered 23/11, 2009 at 23:46 Comment(1)
@RobertVuković I know this is an old question... but see my answer below!Shriver
A
7

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

Arsenic answered 3/12, 2015 at 13:3 Comment(5)
There's no link in my answer @ineersaArsenic
This should be the top answer. I dislike using crappy gui toolsStomato
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.)Scraper
@PeterMortensen Check the documentation here: git-scm.com/docs/git-log#Documentation/git-log.txt---allArsenic
@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.Paulino
D
4

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

Danais answered 23/11, 2009 at 23:54 Comment(1)
Except that the TortoiseSvn shell extensions contain a command for showing the history of a single file, whereas TortoiseGit does not.Stome
S
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"
Shriver answered 23/1, 2015 at 18:27 Comment(0)
L
2

You can use git-diff or git-log.

Loram answered 23/11, 2009 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.