Lesson 3 — Commits, History, and Conventional Commits¶
1. Learning objectives¶
By the end of this lesson, you should understand:
- What a commit is.
- How Git creates commits.
- The difference between staged and unstaged changes.
- How to write meaningful commit messages.
- How to apply Conventional Commits.
- How to inspect commit history.
- How to inspect a specific commit.
- How to amend or revert commits.
- The difference between
resetandrevert.
2. What is a commit?¶
A commit is a recorded snapshot of your repository.
It represents a set of changes at a particular point in time.
A commit contains:
- Commit hash.
- Author.
- Author date.
- Commit message.
- Parent commit references.
- Snapshot information.
A commit does not simply store a copy of every file each time. Git uses an object database and content-addressed objects to represent repository history efficiently.
3. Understand the Git workflow¶
Working Directory
|
| git add
v
Staging Area
|
| git commit
v
Local Repository
|
| git push
v
Remote Repository
Working directory¶
The files you currently edit.
Staging area¶
The changes selected for the next commit.
Local repository¶
The commits stored on your computer.
Remote repository¶
The repository hosted on GitHub or another Git server.
4. Inspect changes before committing¶
Check repository status:
Display unstaged changes:
Display staged changes:
Display a summary of staged changes:
Inspect a particular file:
These commands help prevent accidentally committing unrelated changes.
5. Stage changes¶
Stage a single file:
Stage a directory:
Stage all changes:
Stage only tracked-file modifications and deletions:
Stage interactively:
Interactive staging allows you to select individual change sections rather than staging an entire file.
6. Create commits¶
Create a commit:
Open the configured text editor to write a longer commit message:
A useful commit should represent one logical change.
Avoid combining unrelated work into one commit.
7. Conventional Commits¶
Conventional Commits is a convention for structuring commit messages.
General format:
Examples:
feat(auth): add refresh token support
fix(orders): correct total calculation
docs(api): document authentication endpoints
refactor(users): move validation into services
test(orders): add checkout tests
chore(deps): update dependencies
Common commit types¶
| Type | Purpose |
|---|---|
feat |
Introduces a feature |
fix |
Fixes a bug |
docs |
Documentation changes |
style |
Formatting changes without changing logic |
refactor |
Code restructuring |
test |
Adds or modifies tests |
perf |
Performance improvement |
build |
Build system or dependency changes |
ci |
CI configuration changes |
chore |
General maintenance |
revert |
Reverts a previous commit |
Scope¶
A scope identifies the area affected.
Examples:
feat(auth): add login
fix(payments): handle declined transactions
docs(github): add pull request guide
Breaking changes¶
Use ! to indicate a breaking change:
You can also explain the breaking change in a footer:
Breaking changes should be documented clearly.
8. Good and bad commit messages¶
Bad:
Good:
feat(menu): add product category filtering
fix(auth): prevent expired refresh token reuse
docs(deployment): explain Docker configuration
test(orders): cover empty checkout requests
A good commit message should answer:
- What changed?
- Which area changed?
- Why is the change relevant, if additional explanation is needed?
9. Inspect commit history¶
Show full commit history:
Show a compact history:
Show branches and graph:
Show file change statistics:
Show commits by author:
Show commits from a date range:
Show history for one file:
Show the detailed history of a file, including line changes:
10. Inspect a specific commit¶
Display a commit:
Display only its summary:
Display only the commit message and metadata:
Use a short hash when it uniquely identifies the commit.
11. Amend the latest commit¶
If you forgot to include a file in your latest commit:
This adds the staged changes to the latest commit without changing its message.
Change the latest commit message:
Important:
Amending creates a replacement commit with a new hash.
Avoid amending commits that have already been shared with other developers unless you coordinate the history rewrite.
12. Revert a commit¶
git revert creates a new commit that reverses the changes introduced by an earlier commit.
This is usually safer for changes already pushed to a shared branch because it preserves existing history.
13. Reset commits¶
git reset moves the current branch reference and can change the staging area and working directory.
Soft reset¶
Moves the current branch back one commit while keeping the changes staged.
Mixed reset¶
Moves the branch back one commit and leaves the changes unstaged in the working directory.
This is the default reset mode:
Hard reset¶
Moves the branch back one commit and discards changes in the index and working directory that are affected by the reset.
Warning: --hard can destroy uncommitted work. Do not use it unless you understand the consequences and have preserved anything important.
Reset vs revert¶
| Command | Behavior | Typical use |
|---|---|---|
git reset --soft |
Moves branch; keeps changes staged | Redo latest commit |
git reset --mixed |
Moves branch; keeps changes unstaged | Reorganize local changes |
git reset --hard |
Moves branch and discards affected working changes | Carefully discard local work |
git revert |
Creates a new reversing commit | Undo a shared commit safely |
14. Practical exercise¶
Create a branch:
Create:
Add sections covering:
- What Git is.
- What a commit is.
- How staging works.
- Conventional Commits.
- How to inspect history.
Stage and commit the file:
Add another small section to the same file.
Create a second commit:
Inspect your history:
Inspect the changes introduced by your latest commit:
Push the branch:
15. Completion checklist¶
- [ ] Understand the staging area.
- [ ] Use
git diff. - [ ] Use
git diff --cached. - [ ] Stage individual files.
- [ ] Create commits.
- [ ] Write Conventional Commit messages.
- [ ] Inspect commit history.
- [ ] Inspect individual commits.
- [ ] Understand amend.
- [ ] Understand revert.
- [ ] Understand reset modes.
- [ ] Push commits to GitHub.
16. What comes next?¶
Lesson 4 covers comparing branches, inspecting differences, understanding ahead/behind commits, and using GitHub's Compare feature before opening pull requests.