What are Git hashes?

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


A Git hash, often referred to as a commit hash, is a unique identifier for every single commit in a Git repository. It's generated using the SHA-1 cryptographic hash function, which produces a 40-character string of hexadecimal characters (0-9 and a-f). This hash represents the state of the repository at the time of the commit.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

Git hashes are calculated based on the contents of the files in the commit, the metadata of the commit (like timestamp and author), and the parent commit's hash. This dependency chain ensures that every commit is a snapshot not just of the project at a given time, but of its entire history.

The format of a Git hash is a 40-character hexadecimal number. For example, a commit hash might look like this:

Terminal
a1b2c3d4e5f67890abcdef1234567890abcdef12

This example shows the typical structure of a Git hash code. Each part of the hash is directly derived from the content it represents, making every hash unique to its commit.

  1. Integrity: Git hashes are crucial for ensuring the integrity of your code. If even a single byte in your repository changes, the resulting hash will change, which alerts you to alterations.
  2. Traceability: Every commit in Git can be accessed via its unique hash. This allows developers to track changes, revert to previous states, and understand the history of a project.
  3. Merge and rebase: Git uses these hashes to manage merges and rebases, ensuring that changes are applied correctly across different branches.

To see the hashes associated with each commit, you can use the git log command in your terminal:

Terminal
git log

This command displays a list of recent commits, each with its own hash, author, date, and commit message.

Beyond individual commits, Git uses hashes to form what's called a "hash tree" or "commit tree." This tree structure allows Git to quickly compare different versions of a repository, identify divergences, and merge changes. Each commit points to its parent commits by their hashes, creating a linked list from the current state back to the initial commit.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

Git hashes are not just theoretical but have practical implications. Hashes are how you refer to specific commits when executing commands like git checkout and git diff.

  • Checking out specific versions: You can use a commit hash with git checkout to view the state of your repository at the time of that commit.

    Terminal
    git checkout a1b2c3d4e5f67890abcdef1234567890abcdef12
  • Comparing changes: Developers can compare different commits by using their hashes.

    Terminal
    git diff a1b2c3d4e5f67890abcdef1234567890abcdef12 anotherHashHere
  • Automated systems: Automated systems, such as Continuous Integration (CI) platforms, rely on commit hashes as precise identifiers to reference the specific state of the codebase at a given point in time. When a developer pushes changes to a repository, the CI system triggers a build process using the commit hash associated with those changes, ensuring that the code being built and tested corresponds exactly to the version of the code that triggered the build. This granular level of referencing enables CI systems to provide accurate feedback on the quality and integrity of the codebase, facilitating rapid development iterations and maintaining the reliability of the software.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2