What is a Commit?
A commit is a snapshot of your project at a specific point in time. It records the complete state of all tracked files, along with metadata about who made the changes, when, and why. Commits are the building blocks of Git’s history, forming a directed acyclic graph (DAG) that represents your project’s evolution.Unlike many version control systems that store diffs, Git stores complete snapshots. This makes operations like checkout and branch switching extremely fast.
Anatomy of a Commit
Every commit is a Git object with a unique SHA-1 identifier. Fromcommit.h and the Git data model, commits contain:
Commit Contents
A commit stores these required fields:Tree ID
A reference to the root directory tree object, which represents the complete directory structure and file contents at commit time:
Parent(s)
SHA-1 hash(es) of the parent commit(s):
- First commit: 0 parents
- Regular commit: 1 parent
- Merge commit: 2+ parents
Example Commit Object
Here’s how Git stores a commit internally (viewable withgit cat-file -p <commit-id>):
Creating Commits
Basic Commit Workflow
Fromgit-commit.adoc, the standard workflow is:
Stage your changes
Commit Options
Common Commit Options
Common Commit Options
-a, --all
Automatically stage all modified and deleted files:--amend
Modify the most recent commit:-v, --verbose
Show diff in the commit message editor:--author
Override the author field:Commit Messages
Good commit messages are essential for maintainable projects. Git’s own guidelines fromSubmittingPatches:
Commit History
The Commit Graph
Commits form a directed acyclic graph (DAG) where:- Each commit points to its parent(s)
- The graph has no cycles (you can’t travel back to the same commit)
- Multiple paths can exist (from branches and merges)
glossary-content.adoc:
A directed acyclic graph of commit objects forms the definitive commit graph. This structure enables Git’s powerful history traversal and branching capabilities.
Viewing History
History Viewing Commands
History Viewing Commands
Commit Internals
How Git Stores Commits
When you rungit commit, Git performs these steps (implemented in commit.c):
Create tree objects
Convert the staging area into a tree structure representing directories and files.
Write commit object
Create a commit object containing:
- Tree SHA-1
- Parent commit SHA-1(s)
- Author and committer info
- Commit message
Commit Immutability
Operations that appear to modify commits actually create new ones:git commit --amend: Creates a new commit with the same parentgit rebase: Creates new commits with different parentsgit cherry-pick: Creates new commits with different parents
Special Commit Types
Initial Commit
The first commit in a repository has no parents:Merge Commits
Merge commits have multiple parents, recording where branches joined:F is a merge commit with parents C and E.
Root Commits
Orphan branches start with a root commit (no parent):Commit References
Git provides many ways to reference commits:| Reference | Description | Example |
|---|---|---|
| SHA-1 | Full or abbreviated hash | 750b4ea |
| Branch name | Latest commit on branch | main |
| HEAD | Current commit | HEAD |
| Relative | Parent/ancestor | HEAD^, HEAD~2 |
| Date | By timestamp | main@{2.days.ago} |
| Reflog | By position | main@{1} |
Relative References
Best Practices
Commit early and often
Make small, focused commits rather than large, monolithic ones. Each commit should represent a logical unit of change.
Write clear messages
Your future self (and colleagues) will thank you. Explain why the change was made, not just what changed.
Keep commits atomic
Each commit should be self-contained and should not break the build. This makes
git bisect and cherry-picking more effective.Commit Operations
Amending the Last Commit
Reverting Commits
Create a new commit that undoes changes:Resetting to a Previous Commit
Related Concepts
- Repositories - Where commits are stored
- Branches - Named pointers to commits
- Staging Area - Preparing commits
- Remote Repositories - Sharing commits
Further Reading
git help commit- Complete commit command referencegit help log- Viewing commit historygit help revisions- All ways to specify commits
