如何获取Git中当前提交的哈希?
当前回答
提交哈希
git show -s --format=%H
缩写的提交哈希
git show -s --format=%h
-s标志与--no补丁相同,代表“抑制差异输出”。
单击此处查看更多git显示示例。
其他回答
以下是另一种方法:)
git log | grep -o '\w\{8,\}' | head -n 1
也许你想要一个别名,这样你就不必记住所有漂亮的细节。完成以下步骤之一后,您将能够简单地键入:
$ git lastcommit
49c03fc679ab11534e1b4b35687b1225c365c630
根据公认的答案,有两种设置方法:
1) 通过编辑全局配置(我最初的答案),教git明确的方法:
# open the git config editor
$ git config --global --edit
# in the alias section, add
...
[alias]
lastcommit = rev-parse HEAD
...
2) 或者如果你喜欢一个快捷方式来教git一个快捷方法,正如Adrien最近评论的那样:
$ git config --global alias.lastcommit "rev-parse HEAD"
从这里开始,使用gitlastcommit显示最后一次提交的哈希。
主要git repo和子模块的漂亮打印:
echo "Main GIT repo:"
echo $(git show -s --format=%H) '(main)'
echo "Sub-modules:"
git submodule status | awk '{print $1,$2}'
示例输出:
3a032b0992d7786b00a8822bbcbf192326160cf9 (main)
7de695d58f427c0887b094271ba1ae77a439084f sub-module-1
58f427c0887b01ba1ae77a439084947de695d27f sub-module-2
d58f427c0887de6957b09439084f4271ba1ae77a sub-module-3
如果需要在脚本期间将哈希存储在变量中,可以使用
last_commit=$(git rev-parse HEAD);
或者,如果您只需要前10个字符(如github.com)
last_commit=$(git rev-parse --short=10 HEAD);
我将如何在python中实现(基于@kenorb的bash答案)
def get_git_sha():
# Which branch are we on?
branch = open(".git/HEAD", "r").read()
# Parse output "ref: refs/heads/my_branch" -> my_branch
branch = branch.strip().split("/")[-1]
# What's the latest commit in this branch?
return open(f".git/refs/heads/{branch}").read().strip()
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别