如何找到修改文件的最新 git commit?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4784575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find the most recent git commit that modified a file?
提问by Cory Klein
I want to find the most recent commit that modified a source file.
我想找到修改源文件的最新提交。
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 blame
用来查看每一行的所有提交日期,但很难确切地看到哪个提交是最后一个接触文件的提交。
How can I find the last commit that touched a given file in my git repository?
如何在我的 git 存储库中找到触及给定文件的最后一次提交?
回答by Jo Liss
git log
supports looking at the history of specific files (and directories), so you can call it like this:
git log
支持查看特定文件(和目录)的历史记录,因此您可以这样调用它:
git log my/file.c
If you really only want to list the onemost recent commit, for example to use it in a script, use the -n 1
option:
如果您真的只想列出最近的一次提交,例如在脚本中使用它,请使用以下-n 1
选项:
git log -n 1 --pretty=format:%H -- my/file.c
--pretty=format:%h
tells git log
to show only the commit hash. The --
separater stops the file name from getting interpreted as a commit name, just in case it's ambiguous.
--pretty=format:%h
告诉git log
只显示提交哈希。在--
分体从得到解释为提交的名称停止文件名,以防万一它是不明确的。
回答by Michael Erickson
If you just want to findthe most recent commit, then you don't want git-log
, you want git-rev-list
, which lists the commit objects changing that file, in that commit path, starting with the most recent one (chronologically). Simply put:
如果您只想找到最近的提交,那么您不需要git-log
, 您想要git-rev-list
,它列出了在该提交路径中更改该文件的提交对象,从最近的提交(按时间顺序)开始。简单的说:
git rev-list -1 <commit> <filename>
git rev-list -1 <commit> <filename>
For git-rev-list
in your case, you just supply:
对于git-rev-list
您的情况,您只需提供:
- The number of commits to include, or -1for only the most recent,
- The branch (or commit id) to start looking back from, HEADif you are already on it, or --allif you want all known commits, and
- The relative path to your file.
- 要包含的提交数,或者-1仅表示最近的提交,
- 开始回顾的分支(或提交 ID),如果您已经在上面,则为HEAD,如果您想要所有已知的提交,则为--all,以及
- 文件的相对路径。
This just returns the most recent commit ID in the current branch to alter that file, ex: 215095e2e338525be0baeeebdf66bfbb304e7270
这只是返回当前分支中的最新提交 ID 来更改该文件,例如: 215095e2e338525be0baeeebdf66bfbb304e7270
For a more complex example, you can use tag names, and even remote references, and include relative path names with wildcards, for ex:
对于更复杂的示例,您可以使用标记名称,甚至远程引用,并包括带有通配符的相对路径名称,例如:
git rev-list origin/user/bob/testbranch -1 src/bfiles/*.txt
git rev-list origin/user/bob/testbranch -1 src/bfiles/*.txt
...Which would tell you what the most recent change was to the wildcard match in that branch's history. The options for rev-list are extreme, it is one of the most important plumbing commands, so you can include or exclude by just about any criteria you can imagine.
...这将告诉您该分支历史中通配符匹配的最新变化是什么。rev-list 的选项是极端的,它是最重要的管道命令之一,因此您可以根据您能想象的任何标准包含或排除。
Of course, refer to the git-rev-list(1) Manual Page.
当然,请参考git-rev-list(1) 手册页。
回答by TheBamf
If you want to just get the hash of the latest commit to modify a specific set of files (and want to avoid awk
) you can use:
如果您只想获取最新提交的哈希值来修改一组特定的文件(并想避免awk
),您可以使用:
git log -n 1 --pretty=format:%h -- <path>
This can be useful for getting the commit hash for subsequently using with git describe
.
这对于获取随后使用 with 的提交哈希很有用git describe
。
For example (in case it's useful for anyone)…
例如(以防它对任何人有用)...
I create a current version id by considering the latest commit to change any source file (assuming you mark versions with tags like mycode-1.2.1
):
我通过考虑更改任何源文件的最新提交来创建当前版本 ID(假设您使用标签标记版本mycode-1.2.1
):
COMMIT=$(git log -n 1 --pretty=format:%h -- *.c *.h)
if VN=$(git describe --always --abbrev=5 --match "mycode-*" $COMMIT 2>/dev/null) &&
case "$VN" in
mycode-*)
git update-index -q --refresh
test -z "$(git diff-index --name-only HEAD *.c *.h)" ||
VN="$VN-mod" ;;
*) VN="mycode-unknown-g$VN" ;;
esac
then
continue
else
VN="mycode-unknown"
fi
This produces ids like:
这会产生如下 ID:
mycode-1.2.1
- when the current state of the source files corresponds to a tagged versionmycode-1.2.1-g3k7s2
- when the current state of the source files corresponds to commit following a tagged versionmycode-1.2.1-g3k7s2-mod
- when the current state of the source files have been modified since the last commit following a tagged versionmycode-unknown
- when there has not yet been a version tag created
mycode-1.2.1
- 当源文件的当前状态对应于标记版本时mycode-1.2.1-g3k7s2
- 当源文件的当前状态对应于标记版本之后的提交时mycode-1.2.1-g3k7s2-mod
- 当源文件的当前状态自标记版本之后的上次提交以来已被修改时mycode-unknown
- 当还没有创建版本标签时
回答by Noufal Ibrahim
I'm not sure if this is what you want but if you do a git log <thefile>
to get the commits that altered that file. You can pick the topmost one. It should be the one you're looking for.
我不确定这是否是您想要的,但是如果您执行 agit log <thefile>
来获取更改该文件的提交。你可以选择最上面的一个。它应该是你要找的那个。
回答by fastmultiplication
To get just the ref on one line, try:
要在一行中只获取 ref,请尝试:
git log -n1 --oneline <path> | awk '{print ;}'
回答by Joe
Once you have the SHA id of the commit you want to look at using git log FILENAME
, you should be able to do git show SHA_ID_HERE
to see what you did for that particular commit. You don't even need to enter the entire ID; the first 6 characters should be enough.
一旦获得了要使用的提交的 SHA id git log FILENAME
,您应该能够git show SHA_ID_HERE
查看您为该特定提交所做的工作。您甚至不需要输入整个 ID;前 6 个字符应该足够了。