Skip to main content

What is a Commit?

A commit in a Git repository records a snapshot of all the tracked files in your directory. Think of it as a powerful save point for your project - like a giant copy and paste, but even better.
Commits are the fundamental building blocks of Git. Every operation you perform in Learn Git Branching revolves around creating, moving, or manipulating commits.

How Commits Work

Git keeps commits as lightweight as possible. Rather than blindly copying the entire directory every time you commit, Git can compress a commit as a set of changes (a “delta”) from one version to the next.

Commit History

Git maintains a history of which commits were made when. Most commits have ancestor commits above them - we show this with arrows in the visualization. This parent-child relationship means:
  • Each commit points to its parent(s)
  • Following the arrows backward takes you through the complete history
  • The commit tree flows from the oldest (root) commit to the newest

Commits in the Visualization

Visual Representation

In Learn Git Branching, commits appear as circles with labels:
  • C0 - The initial root commit
  • C1, C2, C3 - Subsequent commits in order
  • Arrows point from child commits to their parents

Creating Commits

1

Run the commit command

Simply type git commit to create a new commit:
git commit
2

Watch the visualization

A new commit appears on top of the current HEAD position, and the current branch pointer moves forward to reference it.

Example: Basic Commits

Starting with this tree:
main
  |
  C1
  |
  C0
After running git commit twice:
main
  |
  C3
  |
  C2
  |
  C1
  |
  C0

Commit Properties

Lightweight Nature

Commits are very lightweight, making switching between them wicked fast. This efficiency allows you to create commits frequently without worrying about performance.

Immutability

Once created, commits are immutable. Operations like rebase don’t modify existing commits - they create copies with new commit IDs:
  • Original: C3
  • Copy: C3' (shown with a prime mark)

Parent Relationships

Most commits have exactly one parent, but:
  • Root commits (C0) have no parents
  • Merge commits have two parents (one from each merged branch)

Commit Messages

While Learn Git Branching focuses on the visual structure, in real Git you can add messages:
git commit -m "Add new feature"
Messages help you track what each commit represents.

Advanced Commit Operations

Relative References

You can reference commits relative to others:
  • HEAD~ - One commit before HEAD
  • HEAD~2 - Two commits before HEAD
  • HEAD^ - The parent of HEAD (same as HEAD~)
  • HEAD^2 - The second parent of a merge commit

Commit Ranges

Commands like cherry-pick and rebase work with ranges:
git cherry-pick C2 C3 C4

Key Concepts from Source Code

Based on how Learn Git Branching implements commits:
// From commits.js level
// Creates new commits on the current branch
var newCommit = engine.commit({
  isAmend: false
});

Learning Path

Introduction Level

The first level “Introduction to Git Commits” teaches:
  1. What commits represent (snapshots of your project)
  2. How commits maintain history through parent pointers
  3. That commits are lightweight and fast
Goal: Create two commits starting from C1:
git commit
git commit
Result: A tree with commits C0 -> C1 -> C2 -> C3

Color Coding

In the visualization, each branch has a unique color. Commits are colored based on which branches contain them, helping you understand branch relationships.

Best Practices

Commit Often

Since commits are lightweight, create them frequently to capture logical units of work.

Follow History

Use the parent arrows to understand how your code evolved over time.

Understand References

Branches and tags are just pointers to commits - the commits themselves are permanent.

Think in Snapshots

Each commit is a complete snapshot, not just a diff. This makes operations like checkout very fast.

Common Patterns

Linear History

The simplest commit structure:
C0 -> C1 -> C2 -> C3
Each commit has exactly one parent, creating a straight line.

Divergent History

When branches exist:
       C3 (main)
      /
C0-C1
      \
       C2 (bugFix)
Both C2 and C3 share parent C1 but are on different branches.
  • Branching - Creating parallel lines of development
  • Merging - Combining commit histories
  • Rebasing - Linearizing commit history

Next Steps

Now that you understand commits, learn how to:
  1. Create branches to develop features independently
  2. Move between commits using checkout
  3. Combine work from different branches using merge or rebase

Build docs developers (and LLMs) love