Making Commits

8.1 What is a Commit in Git?

A commit represents a saved state of your project's files.

Each commit stores:
The changes (diffs) made to files.
Metadata: author, timestamp, commit message.
A unique SHA-1 hash to identify it.
This makes Git's history traceable and tamper-proof.

Example:
1git commit -m "Add login validation feature"

Each commit is like an entry in your project's timeline — a checkpoint you can always return to.

8.2 Writing Good Commit Messages

A well-written commit message explains what and why you did something — not just how.

🔹Example of a Bad Commit:
1git commit -m "update"

🔹Example of a Good Commit:
1git commit -m "Fix bug: prevent empty username submission in signup form"

✍️ Commit Message Tips:
Use present tense ("Add feature" not "Added feature").
Keep the subject line under 50 characters.
Use the body (optional) for explanation:

1git commit

Then type:
1Add input validation for login

- Prevents login with empty fields
- Improves UX by showing proper error message

8.3 Committing Changes

After staging changes with git add, run:
1git commit -m "Describe your change"

To include unstaged files directly (less recommended):
1git commit -am "Quick fix for typo"

The -a flag stages all tracked modified files automatically before committing — but it doesn't include new untracked files.

8.4 Viewing Commit History

You can review past commits to understand how your project evolved.

Basic log view:
1git log

Compact one-line view:
1git log --oneline

Graph view for branches:
1git log --oneline --graph --decorate --all

Each commit shows:
1commit 4a2b9f1 (HEAD -> main) 2Author: John Doe <john@example.com> 3Date: Wed Nov 5 20:13 2025 4 5 Add error handling in payment module

8.5 Amending the Last Commit

You can edit your last commit if you forgot to include something or made a typo.

1git commit --amend

This opens your editor to change the message or add new staged files.
💡 Use this only for local commits that haven't been pushed to a remote repository yet.

8.6 Understanding HEAD

In Git, HEAD is a pointer to your current commit — the "tip" of the branch you're working on.

When you make a new commit:
Git moves HEAD to that new commit.
The previous commit becomes its parent.

You can check this relationship with:
1git log --graph --oneline

8.7 Commit Best Practices

Make small, focused commits — one logical change per commit.
Write meaningful messages — so history is self-explanatory.
Commit often, but not for every keystroke — think in milestones.
Avoid binary files in commits unless necessary (they bloat history).
Don't commit secrets (like passwords or API keys).

8.8 Key Takeaways

A commit records staged changes into Git's permanent history.
Use clear, concise commit messages that explain purpose and context.
Use git commit --m for simple commits, or git commit for detailed ones.
Use git log to review history and git commit --amend to fix the latest commit.
Every commit should represent one logical step in your project's journey.