Skip to main content
Git Basics
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 status to 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 diff to see exact line-by-line changes.

3. Beginner Explanation

Imagine you are packing a suitcase for a trip.
  1. 1. The Bedroom (Working Directory): You have clothes scattered all over your bed.
  1. 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).
  1. 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. 1. Check Status: Always ask Git what is happening.
bash
123456789101112131415161718192021222324252627282930313233343536373839404142
   git status
   ```
   *(Git will list files in red if they are modified but not staged).*

2. **Stage the Files:** Move the files to the Staging Area.
   ```bash
   # Add a specific file
   git add index.html 
   
   # Or, add ALL changed files in the folder at once (The period means "everything here")
   git add .
   ```

3. **Commit the Files:** Take the permanent snapshot. You MUST include a message (`-m`) explaining what you changed.
   ```bash
   git commit -m "Add login button to homepage"
   ```

### 5. Writing Good Commit Messages
A commit message is a note to your future self and your teammates explaining *why* a change was made.
- **Bad:** `git commit -m "fixed stuff"`
- **Bad:** `git commit -m "update"`
- **Good:** `git commit -m "Fix typo in navigation menu"`
- **Good:** `git commit -m "Add responsive CSS to header"`

*Professional Rule: Write commit messages in the imperative mood, as if you are giving a command (e.g., "Add feature" instead of "Added feature").*

### 6. Mini Project: Track Changes in Project Files
Let's create a file, stage it, and commit it.

**Step-by-Step Walkthrough:**
1. Ensure you are inside your Git repository (`cd website`).
2. Create a new file: `echo "<h1>Hello World</h1>" > index.html`
3. Run `git status`. You will see `index.html` is listed in red as "Untracked files".
4. Add it to the staging area: `git add index.html`
5. Run `git status` again. `index.html` is now green ("Changes to be committed").
6. Take the snapshot: `git commit -m "Create initial index.html file"`
7. Run `git status` one last time. It will say "nothing to commit, working tree clean." You have successfully saved your history!

### 7. Seeing What Changed (`git diff`)
If you open `index.html` tomorrow, delete "Hello World", and type "Hello Universe", Git will notice.
Before you `git add` the file, you might want to double-check exactly what you changed.

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 type git commit. Git will say "no changes added to commit". You *must* move the files to the Staging Area first. (Alternatively, you can use the shortcut git commit -am "message" to add and commit tracked files simultaneously).

10. Exercises

  1. 1. What is the purpose of the Staging Area? Why doesn't git commit just save everything in the folder automatically?
  1. 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.

13. Next Chapter Recommendation

Our history is a straight line. But what if two developers want to work on different features at the exact same time without breaking each other's code? Proceed to Chapter 4: Git Branching Fundamentals.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·