如何获取Git中当前提交的哈希值 技术背景 在使用Git进行版本控制时,每个提交都有一个唯一的哈希值,它可以用于标识特定的提交,方便回退、对比和引用等操作。了解如何获取当前提交的哈希值是Git使用中的一个基础技能。
实现步骤 使用 git rev-parse
1 git rev-parse --short HEAD
1 git rev-parse --verify HEAD
使用 git log
1 git log --pretty=format:'%h' -n 1
使用 git show
-s
标志等同于 --no-patch
,表示不显示差异输出。
直接读取文件 .git/refs/heads/master
文件包含 master
分支上最新提交的哈希值,可以直接读取:
1 cat .git/refs/heads/master
但要注意,Git现在支持将一些头引用存储在 pack-ref
文件中,而不是 /refs/heads/
文件夹中的文件。
使用 git describe
默认情况下,git describe
会给出 <最近的标签>-<额外提交数>-<当前提交的哈希值>
的格式:
存储哈希值到变量 在脚本中,可以将哈希值存储到变量中:
1 last_commit=$(git rev-parse HEAD);
如果只需要前10个字符:
1 last_commit=$(git rev-parse --short=10 HEAD);
自定义别名 可以为获取哈希值的命令设置别名,方便使用:
1 git config --global alias.lastcommit "rev-parse HEAD"
之后可以使用 git lastcommit
来显示最后一次提交的哈希值。
检查状态并获取哈希值 可以结合 git status
检查工作目录是否干净,并在哈希值后添加 -dirty
标记:
1 2 3 test -z "$(git status --porcelain) " \ && echo "$(git rev-parse --short HEAD) " \ || echo "$(git rev-parse --short HEAD) -dirty"
核心代码 Bash脚本示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #!/usr/bin/env bash RETURN_CODE_SUCCESS=0 RETURN_CODE_ERROR=1git_get_status () { if [ -z "$(git status --porcelain) " ]; then echo "clean" else echo "dirty" fi }git_get_short_hash () { is_git_repo="$(git rev-parse --is-inside-work-tree) " if [ "$is_git_repo " != "true" ]; then echo "(not a git repo)" return $RETURN_CODE_SUCCESS fi git_commit_hash="$(git rev-parse --short HEAD) " if [ "$(git_get_status) " = "clean" ]; then echo "${git_commit_hash} " else echo "${git_commit_hash} -dirty" fi }print_status_if_dirty () { if [ "$(git_get_status) " = "dirty" ]; then echo "" echo "WARNING: Your git status is dirty! You have uncommitted changes:" echo "" echo "=== git status ===" git status echo "" echo "=== git diff ===" git diff echo "" fi }main () { git_get_short_hash print_status_if_dirty }if [ "${BASH_SOURCE[0]} " = "$0 " ]; then __name__="__main__" else __name__="__source__" fi if [ "$__name__ " = "__main__" ]; then main "$@ " fi
Python示例 1 2 3 4 5 6 7 8 9 10 11 12 13 import git_get_short_hashimport textwrap git_get_short_hash = git_get_short_hash.git_get_short_hash3 git_short_hash = git_get_short_hash() program_info_str = textwrap.dedent(f""" My other program details here... Program version: {git_short_hash} """ )print (program_info_str) mylogfile.write(program_info_str)
最佳实践 对于日常使用,建议使用 git rev-parse HEAD
或 git rev-parse --short HEAD
来获取哈希值,因为它们简洁且高效。 如果需要在脚本中动态获取哈希值并根据状态进行处理,可以使用上述的 git_get_short_hash
函数。 为常用命令设置别名可以提高工作效率,例如 git sha
。 常见问题 文件读取方法失效 :直接读取 .git/refs/heads
文件夹中的文件可能会失败,因为Git现在支持将一些头引用存储在 pack-ref
文件中。分离头指针模式 :在分离头指针模式下,一些基于引用的方法可能会失败,因为 HEAD
不再指向引用,而是直接指向哈希值。性能问题 :如果对性能有较高要求,cat .git/refs/heads/<分支名>
比其他方法要快得多。