如何获取Git中当前提交的哈希?


当前回答

以下是另一种方法:)

git log | grep -o '\w\{8,\}' | head -n 1

其他回答

下面是另一个直接访问实现:

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 show --pretty=%h 

如果您需要哈希的特定位数,可以添加:

--abbrev=n

也总是有数字描述。默认情况下,它为您提供--

john@eleanor:/dev/shm/mpd/ncmpc/pkg (master)$ git describe --always
release-0.19-11-g7a68a75

要将任何扩展对象引用转换为哈希,请使用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。