Exercise: Committing Changes
Practice making commits with good practices.
Setup
bash
1mkdir commit-practice
2cd commit-practice
3git init
4
5echo "# Commit Practice" > README.md
6git add README.md
7git commit -m "Initial commit"Exercise 1: Basic Commit
Task
Create a file and commit it with a descriptive message.
Steps
bash
1# Create a file
2echo "function hello() { return 'Hello World'; }" > app.js
3
4# Stage it
5git add app.js
6
7# Commit with descriptive message
8git commit -m "Add hello function to app.js"
9
10# Verify
11git log --onelineExercise 2: Commit Multiple Files
Task
Create multiple related files and commit them together.
Steps
bash
1# Create related files
2echo "export function login() {}" > auth.js
3echo "export function logout() {}" >> auth.js
4echo ".auth-container { padding: 20px; }" > auth.css
5echo "<!DOCTYPE html><html>Auth Page</html>" > auth.html
6
7# Stage all auth files
8git add auth.js auth.css auth.html
9
10# Commit together
11git commit -m "Add authentication module with styles and template"
12
13# Verify
14git log --onelineExercise 3: Use Commit Types
Task
Make commits using conventional commit types.
Steps
bash
1# Feature commit
2echo "function newFeature() {}" >> app.js
3git add app.js
4git commit -m "feat: Add newFeature function"
5
6# Fix commit
7echo "// Fixed bug" >> app.js
8git add app.js
9git commit -m "fix: Resolve function scope issue"
10
11# Documentation commit
12echo "# App Documentation" > DOCS.md
13git add DOCS.md
14git commit -m "docs: Add initial documentation"
15
16# View history
17git log --onelineExercise 4: Amend a Commit
Task
Make a commit, then amend it to add a forgotten file.
Steps
bash
1# Make a commit
2echo "const config = {};" > config.js
3git add config.js
4git commit -m "Add configuration file"
5
6# Oops! Forgot to add the config documentation
7echo "# Configuration Guide" > CONFIG.md
8
9# Add the forgotten file and amend
10git add CONFIG.md
11git commit --amend --no-edit
12
13# Verify the amended commit includes both files
14git show --name-only HEADExercise 5: Fix Commit Message
Task
Make a commit with a typo, then fix it.
Steps
bash
1# Make a commit with typo
2echo "utils" > utils.js
3git add utils.js
4git commit -m "Add utls file" # Typo!
5
6# Fix the message
7git commit --amend -m "Add utils file"
8
9# Verify
10git log --oneline -1Exercise 6: Commit All Tracked Files
Task
Use -a flag to commit all modified tracked files.
Steps
bash
1# Modify multiple tracked files
2echo "// Updated" >> app.js
3echo "// Updated" >> auth.js
4echo "// Updated" >> utils.js
5
6# Commit all with -a flag
7git commit -am "Update all JavaScript files with comments"
8
9# Verify
10git log --oneline -1
11git show --name-only HEADExercise 7: View Commit Details
Task
Explore different ways to view commit information.
Steps
bash
1# View full log
2git log
3
4# View compact log
5git log --oneline
6
7# View with stats
8git log --stat
9
10# View specific commit
11git show HEAD
12
13# View only file names in commit
14git show --name-only HEAD
15
16# View last 3 commits
17git log -3 --onelineExercise 8: Undo a Commit
Task
Make a commit, then undo it while keeping changes.
Steps
bash
1# Make a commit
2echo "This will be undone" > temp.js
3git add temp.js
4git commit -m "Add temporary file"
5
6# Verify commit exists
7git log --oneline -1
8
9# Undo the commit (keep changes staged)
10git reset --soft HEAD~1
11
12# Verify
13git status
14# Changes to be committed: new file: temp.js
15
16# The commit is gone
17git log --oneline -1Challenge: Complete Feature Workflow
Task
Simulate developing a feature with multiple commits.
Steps
bash
1# 1. Start feature
2git checkout -b feature/user-profile
3
4# 2. Add model
5echo "class User {}" > models/user.js
6mkdir -p models
7echo "class User {}" > models/user.js
8git add models/
9git commit -m "feat: Add User model"
10
11# 3. Add controller
12mkdir -p controllers
13echo "class UserController {}" > controllers/user.js
14git add controllers/
15git commit -m "feat: Add User controller"
16
17# 4. Add route
18mkdir -p routes
19echo "router.get('/user')" > routes/user.js
20git add routes/
21git commit -m "feat: Add User routes"
22
23# 5. Add tests
24mkdir -p tests
25echo "test('user')" > tests/user.test.js
26git add tests/
27git commit -m "test: Add User tests"
28
29# View the feature commits
30git log --onelineSelf-Check Questions
- What flag lets you commit all modified tracked files?
- How do you amend the last commit message?
- How do you undo a commit but keep the changes?
- What makes a good commit message?
Summary
You've practiced:
- ✅ Making basic commits
- ✅ Using descriptive messages
- ✅ Conventional commit types
- ✅ Amending commits
- ✅ Viewing commit history
- ✅ Undoing commits
Great work on committing!
