2

I've been a CVS user in the past, and though I consider Git a good version control system, I am missing a feature I had in CVS. Specifically, I want to see a file's version history, such as the tags on it. How can I do that in Git?

Keith Pinson
  • 7,162
  • 5
  • 54
  • 97
wintige
  • 23
  • 1
  • 3

4 Answers4

5

In git files don't have their own histories. The project as a whole has a history. So the only thing you can ask for is parts of history relevant to a file, or rather (since there is no special concept of file identity) to a path. All the history showing commands accept path-limiter. Like:

$ git log -- file
$ gitk HEAD -- file

The -- separates revisions from paths. You can omit it in most cases, but may be needed if the name is also a valid revision name or if the file does not exist in current version.

You can specify a directory name too, in which case history of all files in that subtree is listed.

Since this limits by path, if the file was renamed, it won't follow. After all, git does not store any explicit information about renames. It can however guess renames and you can ask log to try to follow renames with --follow like:

$ git log --follow -- file

This only works for single file, not a directory.

The other option is looking for changes that contain specific text, which is often easier option if you are looking for when some code comes from. Use the -S option to log like:

$ git log -Stext
Jan Hudec
  • 65,662
  • 12
  • 114
  • 158
3

Rafe Kettler's answer gives you the basics of what you need, but is worth expanding on a bit. Firstly, in case your file has the same name as a branch, you should add -- before the file name:

git log -p -- filename

The -p parameter says to also show the changes to that file that were introduced by that commit. You mention that you would like to see any tags that apply to that version - to do that you should add the --decorate option.

Finally, if that file might have been renamed at some point in its history, or copied into place from another place in the repository, you should consider adding the --follow option.

You can find more information about git log in its documentation, or some other methods of examining the history of a particular file in this other Stack Overflow question:

... which also suggests graphical history browser (e.g. gitk) that can be given a particular path.

Community
  • 1
  • 1
Mark Longair
  • 385,867
  • 66
  • 394
  • 320
2

Try this on for size:

git log -p filename

It's a bit difficult to read on the terminal so you may want to redirect it to a file and look at it there.

Rafe Kettler
  • 69,672
  • 18
  • 145
  • 147
  • thanks. it show the history on commit history. any method can show the information on the tag? commit information is so long and most versions are different from each other. – wintige Mar 07 '11 at 09:20
  • 1
    What is difficult to read? As all git commands, it redirects itself to less, if it's directed to terminal. Of course, the pager is configurable. – Jan Hudec Mar 07 '11 at 10:21
  • @Jan it's easier to read if viewed in an editor that will highlight the different parts of the diff. – Rafe Kettler Mar 07 '11 at 17:44
  • 2
    @Rafe `git config --global color.ui auto` – Dustin Mar 07 '11 at 18:53
  • @wintige example of a diff of a single file between two tags: `git diff 1.6.0..1.6.5 -- Makefile.am` – Dustin Mar 07 '11 at 18:54
0

I'm using git version 2.23.0, and it doesn't need to use '--' to show history on a file if you are not using other options or revision range. Just use:

git log filename