# Committing Changes

> How to save your staged files into a permanent snapshot in the Git history.

Subject: Version Control | Lesson 5 of 29 | 4 min read
Interactive lesson: https://codewithanimation.in/git/git-commit
Previous: [Tracking Changes](https://codewithanimation.in/git/git-add-and-status.md) | Next: [Inspecting History](https://codewithanimation.in/git/git-log.md)

## What is a Commit?

A commit is a permanent snapshot of all your staged files at a specific point in time. It is the fundamental building block of Git's 'Time Machine'.

When you run `git commit`, Git takes everything currently in the Staging Area, packages it into an immutable snapshot, and adds it to the repository's history timeline.

## Why it matters

Commits are your safety net. If you make a terrible mistake tomorrow, you can instantly revert your codebase back to a commit you made today.

Furthermore, every commit requires a 'commit message'. These messages act as a logbook for your project, explaining *why* a change was made to your future self or other developers.

## The structure of a commit

Every commit in Git contains three critical pieces of information:

1. **The Snapshot**: The actual files and folder structure at that exact moment.

2. **The Metadata**: Who made the commit (Author), when they made it (Date), and the commit message.

3. **The Hash**: A unique 40-character alphanumeric ID (like `9fceb02d...`) that acts as the commit's permanent name.

## Common mistake: Vague commit messages

The #1 mistake beginners make is writing terrible commit messages like 'fixed stuff' or 'update'. Six months later, when you're trying to figure out when a bug was introduced, 'fixed stuff' gives you absolutely zero context.

## Tip: The 'Imperative Mood' rule

A professional standard for commit messages is to write them in the imperative mood, as if giving a command. For example, use 'Add login button' instead of 'Added login button' or 'Adding login button'.

## Try it: Commit a staged snapshot

Write a message, commit the staged change and inspect the new history entry in this local simulation.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-commit

## Check yourself

### Question 1

What does the `git commit` command actually do?

- A. It uploads your code to GitHub.
- B. It takes all files from the Working Directory and saves them.
- C. It creates a permanent snapshot of the Staging Area in the repository history.
- D. It creates a new branch for you to work on.

Correct answer: C

A commit packages up exactly what was staged and securely stores it in your local repository timeline forever.

- Why not A: That is `git push`. Committing only saves locally.
- Why not B: It only takes files from the *Staging Area*, not the entire Working Directory.
- Why not D: That is `git branch` or `git switch -c`.

### Question 2

Which of the following is considered a 'good' professional commit message?

- A. Fix bug in header
- B. Fixed the bug in the header
- C. Update files
- D. asdfasdf

Correct answer: A

The standard is the imperative mood ('Fix bug...', 'Add feature...', 'Refactor component...'), which reads like a command to the codebase.

- Why not B: While descriptive, it's not in the imperative mood ('Fix' vs 'Fixed').
- Why not C: This is way too vague. What files? Why?
- Why not D: Never do this.

### Question 3

What is the 'Hash' (or SHA) of a commit?

- A. The password required to unlock the commit.
- B. A unique 40-character ID that acts as the commit's permanent name.
- C. The line of code that caused a merge conflict.
- D. A tag like 'v1.0.0'.

Correct answer: B

Git uses a cryptographic hashing algorithm (SHA-1) to generate a unique string of letters and numbers for every single commit. You use this hash to reference the commit later.

- Why not A: Git commits aren't password protected.
- Why not C: Hashes have nothing to do with specific lines of code.
- Why not D: Tags are human-readable labels that point TO a hash, but they aren't the hash itself.

## Remember this

- `git commit -m 'Message'` creates a snapshot.
- Only staged files are included in the commit.
- Write messages in the imperative mood ('Fix bug' not 'Fixed bug').
