01 / 20

Git Commits:
Saving Your Work

Learn how to commit source code changes with meaningful messages and understand the conceptual workflow that powers modern software development.

JavaScript

What Is Version Control?

Version control is a system that records changes to files over time so you can recall specific versions later. Think of it like an unlimited undo history for your entire project: every change is tracked, every state is recoverable.

What Is a Commit?

A commit is a snapshot of your project at a specific point in time. Each commit records exactly what changed, who changed it, when it happened, and a message explaining why.

Commit A Commit B Commit C Initial setup Add login page Fix button bug

Anatomy of a Commit

Hash (SHA)
A unique ID like a3f7c2d that identifies this exact snapshot
Author
The name and email of the person who made the changes
Timestamp
The exact date and time the commit was created
Message
A human-readable description explaining what and why

The Three Areas of Git

Working Directory Your actual files Edit code here index.js style.css Staging Area Changes ready to be committed index.js Repository Permanent history All past commits git add git commit

The Commit Workflow

1
Edit your files in the working directory: write code, fix bugs, add features.
2
Stage your changes with git add: choose which modified files to include in the next commit.
3
Commit your changes with git commit -m "message": create a permanent snapshot with a description.
4
Repeat: continue editing, staging, and committing as you develop your project.

git add vs git commit

git add git commit
Purpose Select changes to include Save a permanent snapshot
Destination Staging area Repository history
Reversible? Yes: git reset Yes, but more complex
Requires message? No Yes, always
Analogy Packing a box Sealing and labeling the box

Writing Good Commit Messages

A commit message should clearly communicate what changed and why. Future you (and your team) will thank you when reading the project history.

1
Use the imperative mood: "Add login form" not "Added login form"
2
Keep it under 50 characters for the subject line
3
Be specific: describe the change, not just "fix stuff"
4
Explain why, not just what: context matters for future readers

Step-by-Step Commit Workflow

1
Check status: Run git status to see which files have changed.
2
Review changes: Use git diff to inspect what exactly was modified.
3
Stage files: Use git add filename for specific files or git add . for all changes.
4
Commit: Run git commit -m "Your message here" to create the snapshot.
5
Verify: Run git log --oneline to confirm your commit appears in the history.

Example 1: Your First Commit

You just created a new JavaScript file called app.js with a simple function. Let's commit it to the repository.

$ git status
# Untracked files: app.js

$ git add app.js

$ git commit -m "Add greeting function in app.js"

Example 2: Staging Multiple Files

You modified index.html and style.css to add a navigation bar. You want both changes in one commit.

$ git status
# Modified: index.html, style.css

$ git add index.html style.css

$ git commit -m "Add responsive navigation bar"

Example 3: Selective Staging

You changed three files, but only two changes are related. You want to separate unrelated changes into different commits.

$ git status
# Modified: auth.js, login.html, README.md

# Commit 1: Auth feature changes
$ git add auth.js login.html
$ git commit -m "Implement user authentication flow"

# Commit 2: Documentation update
$ git add README.md
$ git commit -m "Update README with setup instructions"

Viewing Commit History

$ git log --oneline

f3a8c21 Update README with setup instructions
b7d9e04 Implement user authentication flow
a1c5f78 Add responsive navigation bar
e9b2d36 Add greeting function in app.js
e9b2d36 oldest a1c5f78 b7d9e04 f3a8c21 HEAD

Common Mistakes

✕ Vague Messages
Messages like "fix stuff" or "update" tell nothing about what changed or why.
✕ Giant Commits
Committing 20 unrelated changes at once makes history impossible to navigate or debug.
✕ Forgetting to Stage
Running git commit without git add first results in an empty commit or missing files.

Bad vs Good Messages

Bad Message Good Message
Fixing a bug "fix" "Fix null check in user login"
Adding a feature "new stuff" "Add search bar to homepage"
Updating styles "css" "Style contact form for mobile"
Removing code "cleanup" "Remove deprecated API helpers"

Question 1 of 5

What does the git add command do?

Question 2 of 5

Which of the following is the best commit message?

Question 3 of 5

You modified three files: auth.js, login.html, and README.md. The first two are related to a login feature; the README is a documentation fix. What is the best approach?

Question 4 of 5

What are the three areas in the Git workflow, in order?

Question 5 of 5

What uniquely identifies every commit in Git?