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.
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.
Anatomy of a Commit
The Three Areas of Git
The Commit Workflow
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.
Step-by-Step Commit Workflow
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.
# 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.
# 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.
# 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
f3a8c21 Update README with setup instructions
b7d9e04 Implement user authentication flow
a1c5f78 Add responsive navigation bar
e9b2d36 Add greeting function in app.js
Common Mistakes
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?