---
title: "特殊的 Git hash"
url: "https://versun.me/special-git-hash"
markdown_url: "https://versun.me/special-git-hash.md"
format: "note"
published: "2024-12-23T06:58:40.000Z"
updated: "2026-01-05T05:29:32.000Z"
collections:
  - "Blog"
site: "Versun"
---

# 特殊的 Git hash

今天AI写了一段神奇的代码：

`parent = commit.parents[0] if commit.parents else self.repo.tree("4b825dc642cb6eb9a060e54bf8d69288fbee4904")`

功能是获取某个git提交的上一个提交，如果没有则返回`4b825dc642cb6eb9a060e54bf8d69288fbee4904`提交。  
刚开始看到硬编码的hash以为又是ai乱写的，所以询问了下ai，才得知：

- `4b825dc642cb6eb9a060e54bf8d69288fbee4904`是一个特殊的 Git hash，代表一个空树（empty tree）
- 这个 hash 是固定的，在所有 Git 仓库中都是一样的
- 当我们需要比较第一个 commit 的变更时，就需要和这个空树比较，因为第一个 commit 之前没有任何内容

我在本地的多个git repo中测试了，还真是一个有效的hash，均返回空，神奇
