Skip
Arish's avatar

25. How to Commit Changes


Committing Changes

A commit is a snapshot of your project at a specific point in time. It's the fundamental unit of change in Git.

Basic Commit

bash
1# Stage changes
2git add filename.js
3
4# Commit with message
5git commit -m "Add user authentication feature"

Commit Messages

Writing Good Commit Messages

Good commit messages are crucial for project history:

bash
1# Good examples
2git commit -m "Add password reset functionality"
3git commit -m "Fix login button not responding on mobile"
4git commit -m "Update user profile validation rules"
5
6# Bad examples
7git commit -m "fix"
8git commit -m "update"
9git commit -m "changes"
10git commit -m "asdfasdf"

Commit Message Format

<type>: <subject> <body> <footer>

Example with Full Format

bash
1git commit -m "feat: Add user authentication
2
3- Implement login/logout functionality
4- Add password hashing with bcrypt
5- Create session management
6
7Closes #123"

Common Commit Types

TypeDescription
featNew feature
fixBug fix
docsDocumentation
styleFormatting (no code change)
refactorCode restructuring
testAdding tests
choreMaintenance tasks

Commit Options

Commit All Tracked Changes

bash
1# Skip staging, commit all modified tracked files
2git commit -a -m "Update all configuration files"
3
4# Or combined
5git commit -am "Update all configuration files"

Note: This doesn't include untracked (new) files!

Multi-line Commit Message

bash
1# Opens your default editor
2git commit
3
4# In the editor, write:
5# First line: Subject (50 chars or less)
6# Blank line
7# Body: Detailed explanation

Amend Last Commit

bash
1# Fix the last commit message
2git commit --amend -m "Corrected commit message"
3
4# Add forgotten files to last commit
5git add forgotten-file.js
6git commit --amend --no-edit

Warning: Don't amend commits that have been pushed!

Viewing Commits

View Commit History

bash
1# Full log
2git log
3
4# Compact log
5git log --oneline
6
7# With graph
8git log --oneline --graph --all
9
10# Last N commits
11git log -5

View Specific Commit

bash
1# Show commit details
2git show abc1234
3
4# Show files changed
5git show --name-only abc1234

Commit Best Practices

1. Commit Often

Make small, frequent commits:

bash
1# Good: Logical units of work
2git commit -m "Add user model"
3git commit -m "Add user controller"
4git commit -m "Add user routes"
5
6# Bad: Everything at once
7git commit -m "Add entire user module"

Group related changes together:

bash
1# Good: Related changes
2git add src/auth/login.js src/auth/logout.js
3git commit -m "Add authentication functions"
4
5# Bad: Unrelated changes
6git add login.js styles.css docs/readme.md
7git commit -m "Various updates"

3. Don't Commit Half-Done Work

Each commit should be a complete unit:

bash
1# Make sure code works before committing
2npm test
3git commit -m "Add search functionality"

4. Test Before Committing

bash
1# Run tests
2npm test
3
4# If tests pass, commit
5git commit -m "Add feature with passing tests"

Undo Commits

Undo Last Commit (Keep Changes)

bash
1# Move changes back to staging
2git reset --soft HEAD~1
3
4# Move changes back to working directory
5git reset HEAD~1
6# or
7git reset --mixed HEAD~1

Undo Last Commit (Discard Changes)

bash
1# Dangerous! Loses changes
2git reset --hard HEAD~1

Revert a Commit (Safe for Shared History)

bash
1# Create a new commit that undoes changes
2git revert abc1234

Commit Workflow Example

bash
1# 1. Make changes
2echo "New feature" >> feature.js
3
4# 2. Check what changed
5git status
6git diff
7
8# 3. Stage specific files
9git add feature.js
10
11# 4. Review staged changes
12git diff --staged
13
14# 5. Commit with descriptive message
15git commit -m "feat: Add new feature functionality"
16
17# 6. Verify commit
18git log -1

Summary

Committing in Git:

  • ✅ Creates a snapshot of your project
  • ✅ Requires meaningful commit messages
  • ✅ Should be done frequently
  • ✅ Groups related changes together
  • ✅ Can be amended or reverted

Practice committing in the next exercise!