问题描述
我想找到修改源文件的最新提交。
I want to find the most recent commit that modified a source file.
我可以使用 git blame
来查看每行提交的所有日期,但很难准确地看到哪个提交是最后一个触及该文件的文件。
I can use git blame
to see all the dates for commits by each line, but it’s difficult to see exactly which commit was the last one to touch the file.
如何在git存储库中找到触及给定文件的最后一个提交?
How can I find the last commit that touched a given file in my git repository?
推荐答案
支持查看特定文件(和目录)的历史记录,因此您可以编写它:
git log
supports looking at the history of specific files (and directories), so you can write this:
git log my/file.c
如果您真的只想列出例如在脚本中使用它,您可以使用 -n 1
:
If you really only want to list the one most recent commit, for example to use it in a script, you can use -n 1
:
git log -n 1 --pretty=format:%H -- my/file.c
- pretty = format:%h
告诉 git log
只显示提交哈希(感谢TheBamf)。 -
separater停止将文件名解释为提交名称,以防万一不明确。
--pretty=format:%h
tells git log
to show only the commit hash (thanks to the TheBamf). The --
separater stops the file name from getting interpreted as a commit name, just in case it's ambiguous.
这篇关于我如何找到修改文件的最新git提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!