Programming Search⌘K

Featurecore

git commit

Also called commit, revision, changeset, check-in, changelist.

A commit is a saved snapshot of the whole project, with an author, a date, a message and links to its parent commits. Git names each commit by the hash of its bytes.

Tool
Git

Synopsis

A version control system keeps the history of a set of files. The unit of that history is the commit: one recorded state of all the files, with a note about who made it, when and why. Other systems call it a revision, a changeset, a check-in or a changelist. There are two ways to store such a history. A delta-based system stores each file as a list of changes over time. A snapshot-based system stores the whole state of the project at each commit, and finds the changes by comparing two snapshots. Git works the second way.1

The forms of git commit that this page uses:

git commit -m <message>          record the index as a commit, with this message
git commit -a -m <message>       first stage every change to a tracked file, then commit
git commit <path>...             commit the listed tracked files as they are in the working tree
git commit --amend [--no-edit]   replace the tip of the branch with a corrected commit
git commit --fixup <commit>      make a "fixup!" commit for a later rebase --autosquash
git commit --allow-empty -m <m>  record a commit whose tree equals its parent's tree

The commands that read a commit back:

git cat-file -p <commit>         print the raw commit object
git show <commit>                print the message and the diff against the parent
git log [--oneline] [--graph]    walk the history back through the parent links

Semantics

r1. git commit records the content of the index, not of the working tree. The index is the staging area that git add fills. A change that is in the working tree but not in the index is not part of the commit. The -a option stages every change to a file that Git already tracks, and then commits.2 Example: first-commit.

r2. A commit object holds the name of one tree object, the names of zero or more parent commits, an author line, a committer line and a message. The tree is the top directory of the snapshot. Each author and committer line has a name, an email address, a time in seconds since 1970 and a time zone offset.3 4 Example: first-commit.

r3. The first commit of a repository has no parent. An ordinary commit has one parent. A merge commit has two or more. The parent links point back in time, so the commits form a directed acyclic graph.4

r4. The name of an object is a hash of its type, its size in bytes, a null byte and its content. In a default repository the hash is SHA-1, and the name is 40 hexadecimal digits.3 5 Example: hash.

r5. Because the commit object holds the names of its tree and its parents, the name of a commit depends on the whole snapshot and on the whole history behind it. A change to any file, to the message, to a date or to any earlier commit gives a different name.6 Example: hash.

r6. A commit object never changes. git commit --amend makes another commit with the same parents and author, and moves the branch to it. The replaced commit stays in the object store, and the reflog still records it.7 8 Example: hash.

r7. After the commit, the current branch points to it. The commit is a child of the commit that HEAD named before. When HEAD is detached, HEAD itself moves and no branch changes.2

r8. A snapshot does not mean a full copy on disk. A file that did not change has the same blob, and both trees name that one blob. When Git packs objects, it can store an object as a delta against a similar object.9 10 Example: storage.

Examples

The examples on this page ran with the Git shown in the first output. Each one starts in an empty directory. The export lines fix the identity and the dates, so the hashes on this page are the hashes that you get. GIT_CONFIG_GLOBAL=/dev/null keeps your own settings out of the run.

first-commit

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
git --version
printf 'apples\nbread\n' > list.txt
git add list.txt
git commit -m "Add the shopping list"
printf 'milk\n' >> list.txt
git commit -a -m "Add milk"
git cat-file -p HEAD
Output
git version 2.56.0
[main (root-commit) e2e401f] Add the shopping list
 1 file changed, 2 insertions(+)
 create mode 100644 list.txt
[main da1f0d3] Add milk
 1 file changed, 1 insertion(+)
tree ccf3dd9e458e303832d744e188ddedae774c3686
parent e2e401f61d550716e959afda92e41bba5a84c675
author Ada <ada@example.com> 1767607200 +0000
committer Ada <ada@example.com> 1767607200 +0000

Add milk

The second commit names one tree and one parent. The parent is the first commit, e2e401f. The number 1767607200 is the time 2026-01-05 10:00:00 UTC in seconds since 1970.

hash

The name of a commit is the hash of its bytes. git hash-object computes the hash of the raw commit again, and the result is the same name. A change of one second in the committer date gives a different commit.

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\nbread\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git rev-parse HEAD
git cat-file commit HEAD | git hash-object -t commit --stdin
git cat-file -s HEAD
GIT_COMMITTER_DATE=2026-01-05T10:00:01Z git commit -q --amend --no-edit
git rev-parse HEAD
git reflog
Output
e2e401f61d550716e959afda92e41bba5a84c675
e2e401f61d550716e959afda92e41bba5a84c675
164
393bf1779a4d6bcfbc7ea156957fe72eb6dea193
393bf17 HEAD@{0}: commit (amend): Add the shopping list
e2e401f HEAD@{1}: commit (initial): Add the shopping list

The commit object is 164 bytes long. The amend did not change the first commit. It made a second commit, and the reflog keeps both names.

storage

Two commits share every file that did not change. Here list.txt has the same blob, 950a188, in both trees. Before packing, the repository holds 7 loose objects: 2 commits, 2 trees and 3 blobs. After git gc, the pack stores the old numbers.txt as a delta of 17 bytes against the changed one, at depth 1.

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
seq 1 2000 > numbers.txt
printf 'apples\n' > list.txt
git add .
git commit -q -m "Add two files"
sed 's/^1000$/one thousand/' numbers.txt > tmp && mv tmp numbers.txt
git commit -q -a -m "Spell out one thousand"
git ls-tree HEAD~1
git ls-tree HEAD
git count-objects -v | head -1
git gc -q
git verify-pack -v .git/objects/pack/pack-*.idx | grep blob
Output
100644 blob 950a188f0a4224ce6a1ac710915113df972aeb2c	list.txt
100644 blob 7972c09aa90a9b3d8519064681f2cca009f8777c	numbers.txt
100644 blob 950a188f0a4224ce6a1ac710915113df972aeb2c	list.txt
100644 blob dbd76b1c15c7f2996384aad22a122e845315cbd9	numbers.txt
count: 7
950a188f0a4224ce6a1ac710915113df972aeb2c blob   7 16 282
dbd76b1c15c7f2996384aad22a122e845315cbd9 blob   8901 4235 298
7972c09aa90a9b3d8519064681f2cca009f8777c blob   17 29 4693 1 dbd76b1c15c7f2996384aad22a122e845315cbd9

So Git has a snapshot model and delta storage at the same time. The model decides what a commit means. The storage only decides how many bytes it takes.

Versions

The commit format has not changed since 2005. Two changes concern the hash. Git 2.13 made the default SHA-1 code one that detects the known collision attack.11 Git 2.29 can create SHA-256 repositories with git init --object-format=sha256, and the documentation marks this as experimental.12 A SHA-256 repository cannot exchange objects with a SHA-1 repository.13

Pitfalls

A change that is not staged is not committed

git commit with no option records the index. A file that you changed but did not add stays out, and Git refuses to make a commit that changes nothing.

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
printf 'bread\n' >> list.txt
git commit -m "Add bread" || echo "exit status $?"
Output
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   list.txt

no changes added to commit (use "git add" and/or "git commit -a")
exit status 1

Run git add list.txt first, or use git commit -a.

A later commit does not remove a file from history

Each commit keeps its own snapshot. When you commit a secret and then delete it in the next commit, the older snapshot still holds it. Any clone of the repository can read it back.

This code runs but gives a wrong answer on purpose.

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'token=s3cr3t\n' > settings.ini
git add settings.ini
git commit -q -m "Add the settings"
git rm -q settings.ini
git commit -q -m "Remove the settings"
git ls-tree HEAD
git show HEAD~1:settings.ini
Output
token=s3cr3t

The tree of HEAD is empty, but HEAD~1 still has the token. Treat a committed secret as public and replace it. Removing it from history means rewriting every later commit, which gives every later commit a different name (r5).

Amend rewrites a commit that others may have

--amend gives the tip of the branch a different name (r6). When the replaced commit was already pushed, other people still have it. The rebase page explains what happens then.

Comparison

Mercurial also records each commit, called a changeset, under a 40-digit hexadecimal ID. It stores each file in a revlog, as a full version or as a compressed delta against the version before, so it is delta-based on disk.14 It also gives each changeset a short revision number, which is local to one repository and can differ between clones.15

Subversion has one central repository. Each commit gets the next global revision number, and each revision number names a tree that is a snapshot of the whole repository. The server assigns the number, so a commit needs the server.16

Fossil calls a commit a check-in and stores it in an SQLite database. Fossil first named its artifacts by SHA1 hashes. Fossil 2.0 learned to read SHA3-256 names, and a repository made with fossil new uses them by default. Old check-ins keep their SHA1 names.17 By default, autosync sends each commit at once to the repository that it was cloned from.18

Jujutsu commits the working copy automatically, so there is no staging area and no separate commit step.19 Each commit has a commit ID, which is a hash like Git's, and a change ID. The change ID stays the same when the commit is rewritten.20

Perforce (P4) groups the files of a commit in a changelist. The server numbers the changelist, and p4 submit saves all of its files in the depot or none of them.21

Implementation notes

The first commit of Git, e83c516 of 7 April 2005, already describes an object store that names each object by the SHA-1 hash of its content, with blobs, trees and a changeset object that holds a tree and its parents.22 A commit is stored as a loose object in .git/objects until git gc or a transfer packs it. The pack format stores some objects as deltas against other objects in the same pack, with two kinds of base: an offset in the pack or an object name.10

References

  1. Scott Chacon and Ben Straub, Pro Git, 2nd edition, What is Git?, Snapshots, Not Differences
  2. Git documentation, git-commit(1), DESCRIPTION
  3. Scott Chacon and Ben Straub, Pro Git, 2nd edition, Git Internals, Git Objects, Tree Objects; Commit Objects; Object Storage
  4. Git documentation, gitglossary(7), commit object; parent
  5. Git documentation, git-hash-object(1), DESCRIPTION
  6. Git documentation, hash-function-transition, Background; Objective
  7. Git documentation, git-commit(1), OPTIONS, --amend
  8. Git documentation, git-reflog(1), DESCRIPTION
  9. Scott Chacon and Ben Straub, Pro Git, 2nd edition, Git Internals, Packfiles, Packfiles
  10. Git documentation, gitformat-pack(5), Deltified representation
  11. Git 2.13 Release Notes, the SHA-1 implementation that detects attempts to create collisions, by Marc Stevens and Dan Shumow, made the default
  12. Git 2.29 Release Notes, SHA-256 support marked as experimental in the documentation
  13. Git documentation, git-init(1), OPTIONS, --object-format
  14. Mercurial wiki, Revlog, the first section (full versions or compressed deltas)
  15. Mercurial wiki, RevisionNumber, the first section (a strictly local identifier)
  16. Version Control with Subversion, for Subversion 1.7, Chapter 1, Revisions
  17. Fossil documentation, Hash Policy, the history of SHA1 and SHA3-256 artifact names
  18. Fossil documentation, Fossil Versus Git, 2.0 Differences Between Fossil And Git, Sync over push
  19. Jujutsu documentation, Comparison with Git, Overview, the working copy is automatically committed
  20. Jujutsu documentation, Glossary, Change ID; Commit ID
  21. P4 CLI Documentation (formerly Helix Core), p4 submit, Description (the atomic submit of a changelist)
  22. Linus Torvalds, Initial revision of "git", the information manager from hell, commit e83c516 of git/git, 7 April 2005, README, the description of the object database

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/QFRVVY/text.yaml