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 commitC1,C2,C3- Subsequent commits in order- Arrows point from child commits to their parents
Creating Commits
Example: Basic Commits
Starting with this tree:git commit twice:
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:Advanced Commit Operations
Relative References
You can reference commits relative to others:HEAD~- One commit before HEADHEAD~2- Two commits before HEADHEAD^- The parent of HEAD (same asHEAD~)HEAD^2- The second parent of a merge commit
Commit Ranges
Commands like cherry-pick and rebase work with ranges:Key Concepts from Source Code
Based on how Learn Git Branching implements commits:Learning Path
Introduction Level
The first level “Introduction to Git Commits” teaches:- What commits represent (snapshots of your project)
- How commits maintain history through parent pointers
- That commits are lightweight and fast
C1:
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:Divergent History
When branches exist:C2 and C3 share parent C1 but are on different branches.
Related Concepts
- Branching - Creating parallel lines of development
- Merging - Combining commit histories
- Rebasing - Linearizing commit history
Next Steps
Now that you understand commits, learn how to:- Create branches to develop features independently
- Move between commits using checkout
- Combine work from different branches using merge or rebase