CHAPTER 03
Git Commits and Tracking Changes
Updated: May 15, 2026
20 min read
# CHAPTER 3
Git Commits and Tracking Changes
1. Introduction
In the previous chapter, we set up our art studio and our easel. Now it is time to paint. In Git, creating a file doesn't automatically back it up; you must explicitly tell Git to take a snapshot of it. This snapshot process is broken into two distinct steps: "Adding" files to the Staging Area, and "Committing" them to the permanent history. In this chapter, we will learn how to write code, track its modifications, and write professional commit messages that document the evolution of our project.2. Learning Objectives
By the end of this chapter, you will be able to:-
Use
git statusto monitor the state of your files.
-
Move files from the Working Directory to the Staging Area using
git add.
-
Permanently save snapshots using
git commit.
- Write clear, professional commit messages.
-
Use
git diffto see exact line-by-line changes.
3. Beginner Explanation
Imagine you are packing a suitcase for a trip.- 1. The Bedroom (Working Directory): You have clothes scattered all over your bed.
-
2.
The Suitcase (Staging Area): You pick up a shirt and put it into the suitcase. You pick up pants and put them in. They aren't traveling yet; you are just organizing what *will* go. (This is
git add).
-
3.
Zipping the Suitcase (The Commit): You close the suitcase, zip it up, and slap a label on it that says "Hawaii Trip 2024". The bag is now locked and ready. (This is
git commit).
If you forget to git add a file, it gets left on the bed and doesn't get packed in the snapshot.
4. The Core Git Workflow
Let's walk through the exact commands you will type 50 times a day as a software developer.- 1. Check Status: Always ask Git what is happening.
bash
bash
git diff
``
Git will output the exact code changes in green (additions) and red (deletions), allowing you to review your code before committing.
8. Best Practices
- Commit Often: Don't work for 8 hours and make one massive commit named "Did all the work." If something breaks, you won't know which part caused the error. Make small, logical commits. Commit when you finish the header. Commit again when you finish the footer.
9. Common Mistakes
-
Forgetting to Add: Beginners often edit a file, skip the git add
command, and immediately typegit commit. Git will say "no changes added to commit". You *must* move the files to the Staging Area first. (Alternatively, you can use the shortcutgit commit -am "message"to add and commit tracked files simultaneously).
10. Exercises
-
1.
What is the purpose of the Staging Area? Why doesn't git commit
just save everything in the folder automatically?
- 2. Write a professional commit message for a code change that fixes a broken login form.
11. FAQs
Q: Can I change a commit message after I hit Enter? A: Yes! If you made a typo in your last commit message, you can type git commit --amend -m "New corrected message". (Warning: Only do this if you haven't pushed the code to GitHub yet).
12. Summary
In Chapter 3, we mastered the daily rhythm of version control. We learned how to write code in our Working Directory, meticulously organize our desired changes using git add to move them to the Staging Area, and finally cement those changes into immutable history using git commit. By establishing the habit of writing clear, imperative commit messages and reviewing our code with git diff`, we ensure that our project's history remains readable, professional, and easily navigable.