开发者

How to have Git show a file's version history

开发者 https://www.devze.com 2023-02-15 10:06 出处:网络
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 ta开

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 ta开发者_如何转开发gs on it. How can I do that in Git?


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


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:

  • How to view file history in Git?

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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号