如何获取Git中当前提交的哈希?
当前回答
以下是Bashshell中使用直接从git文件读取的一行代码:
(head=($(<.git/HEAD)); cat .git/${head[1]})
您需要在git根文件夹中运行上述命令。
当您有存储库文件,但尚未安装git命令时,此方法很有用。
如果不起作用,请检查.git/refs/heads文件夹中您有什么样的头。
其他回答
git show-ref --head --hash head
如果你追求速度,Deestan提到的方法
cat .git/refs/heads/<branch-name>
比这里列出的任何其他方法都快得多。
要将任何扩展对象引用转换为哈希,请使用git-rev-parse:
git rev-parse HEAD
or
git rev-parse --verify HEAD
要检索短哈希:
git rev-parse --short HEAD
要将引用(例如分支和标记)转换为哈希,请使用git show ref和git for each-ref。
我将如何在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()
echo“正在打印当前分支的上次提交id#:”;
git reflog
在文件“.gitconfig”的主目录中添加以下内容
[alias]
sha = rev-parse HEAD
那么您将有一个更容易记住的命令:
$ git sha
59fbfdbadb43ad0b6154c982c997041e9e53b600
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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之间的区别