如何让 Git 显示文件的版本历史记录

发布于 2024-10-20 12:42:56 字数 106 浏览 4 评论 0原文

我过去一直是 CVS 用户,虽然我认为 Git 是一个很好的版本控制系统,但我缺少 CVS 中的一个功能。具体来说,我想查看文件的版本历史记录,例如文件上的标签。我怎样才能在 Git 中做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怀念你的温柔 2024-10-27 12:42:56

在 git 文件中没有自己的历史记录。整个项目有一个历史。因此,您唯一可以要求的是与文件相关的历史记录部分,或者更确切地说(因为没有文件标识的特殊概念)与路径相关的部分历史记录。所有显示命令的历史记录都接受路径限制器。例如:

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

-- 将修订版本与路径分开。在大多数情况下,您可以省略它,但如果该名称也是有效的修订名称或者当前版本中不存在该文件,则可能需要它。

您也可以指定目录名称,在这种情况下,会列出该子树中所有文件的历史记录。

由于这受到路径的限制,如果文件被重命名,它将不会跟随。毕竟,git 不存储任何有关重命名的显式信息。但是,它可以猜测重命名,您可以要求 log 尝试使用 --follow 来跟踪重命名,例如:

$ git log --follow -- file

这仅适用于单个文件,不适用于目录。

另一个选项是查找包含特定文本的更改,如果您正在查找某些代码的来源,这通常是更容易的选项。使用 -S 选项进行日志记录,如下所示:

$ git log -Stext

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
浅沫记忆 2024-10-27 12:42:56

Rafe Kettler 的回答 为您提供了所需的基础知识,但值得进一步扩展。首先,如果您的文件与分支同名,您应该在文件名前添加 --

git log -p -- filename

-p 参数表示还显示对该分支的更改该提交引入的文件。您提到您希望查看适用于该版本的任何标签 - 为此,您应该添加 --decorate 选项。

最后,如果该文件可能在其历史记录中的某个时刻被重命名,或者从存储库中的另一个位置复制到适当的位置,您应该考虑添加 --follow 选项。

您可以在其文档中找到有关 git log 的更多信息,或在其他 Stack Overflow 问题中检查特定文件的历史记录的一些其他方法:

...这还建议使用可以指定特定路径的图形历史记录浏览器(例如 gitk)。

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.

明天过后 2024-10-27 12:42:56

尝试一下大小:

git log -p filename

在终端上阅读有点困难,因此您可能需要将其重定向到文件并在那里查看。

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.

陪你到最终 2024-10-27 12:42:56

我使用的是 git 版本 2.23.0,如果您不使用其他选项或修订范围,则不需要使用“--”来显示文件的历史记录。只需使用:

git log filename

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文