如何获取Git中当前提交的哈希?
当前回答
也总是有数字描述。默认情况下,它为您提供--
john@eleanor:/dev/shm/mpd/ncmpc/pkg (master)$ git describe --always
release-0.19-11-g7a68a75
其他回答
为了完整起见,因为还没有人提出过。git/refs/heads/master是一个只包含一行的文件:master上最新提交的哈希。所以你可以从那里读出来。
或者,作为命令:
cat .git/refs/heads/master
更新:
注意,git现在支持将一些head-ref存储在pack-ref文件中,而不是作为/refs/heads/文件夹中的文件。https://www.kernel.org/pub/software/scm/git/docs/git-pack-refs.html
另一个,使用git-log:
git log -1 --format="%H"
它与“outofculture”非常相似,不过稍短一些。
也许你想要一个别名,这样你就不必记住所有漂亮的细节。完成以下步骤之一后,您将能够简单地键入:
$ 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显示最后一次提交的哈希。
下面是另一个直接访问实现:
head="$(cat ".git/HEAD")"
while [ "$head" != "${head#ref: }" ]; do
head="$(cat ".git/${head#ref: }")"
done
这也适用于http,这对本地包存档很有用(我知道:对于公共网站,不建议将.git目录设置为可访问):
head="$(curl -s "$baseurl/.git/HEAD")"
while [ "$head" != "${head#ref: }" ]; do
head="$(curl -s "$baseurl/.git/${head#ref: }")"
done
使用git rev list--max count=1 HEAD
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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之间的区别