01 / 21

Git Commits
for C++ Projects

Learn how to save snapshots of your C++ code with meaningful commit messages, build a clean project history, and collaborate with confidence.

C++ Version Control

What is a Git Commit?

A commit is a snapshot of your entire project at a specific moment in time. Think of it as pressing "save" in a video game: you can always return to that exact state later.

Snapshot
Records every file's state at that moment
Message
A human-readable note explaining the change
Hash ID
A unique identifier (like a3f7b2c) for each commit

Why Commits Matter

Imagine writing a C++ program for weeks without any save points. One bad change could wipe out everything. Commits solve this by creating a timeline of your work that you can navigate, inspect, and reverse.

1
Undo mistakes: revert to any previous commit if something breaks
2
Track progress: see exactly what changed, when, and why
3
Collaborate safely: multiple developers can work without overwriting each other

The Three Areas of Git

Working Directory Your files on disk Edit freely here git add Staging Area Changes ready to be committed git commit Repository Permanent history of all commits

Anatomy of a Commit

COMMIT OBJECT Hash: a3f7b2c4e8d1... Author: Jane Doe <jane@school.edu> Date: 2024-11-15 14:32:07 Parent: b5e9a1f... (previous commit) Message: "Add sorting algorithm to utils"

The Commit Command

# Stage specific files
$ git add main.cpp utils.h

# Stage all changes in the project
$ git add .

# Commit with a message
$ git commit -m "Implement bubble sort in utils.cpp"

Good vs Bad Messages

Bad Message Good Message
Fix a bug "fixed stuff" "Fix off-by-one error in loop bounds"
Add a feature "update" "Add binary search to sorted array"
Refactor code "changes" "Extract file I/O into FileManager class"
Delete dead code "cleanup" "Remove unused legacy parser functions"

The Commit Message Formula

Verb + What changed + Where / Why (optional) Fix memory leak in LinkedList destructor
1
Start with an imperative verb: Add, Fix, Remove, Refactor, Update, Implement
2
Describe the change concretely: not "stuff", but the actual thing you changed
3
Keep under 72 characters: short enough to scan in git log

git add vs git commit

git add
Moves changes to the staging area. You pick exactly which files or hunks to include. Nothing is permanent yet.
git commit
Takes everything in staging and writes it to the repository history. This is the permanent snapshot with your message.

How to Apply the Workflow

1
Write or edit your C++ code in your editor (main.cpp, utils.h, etc.)
2
Check status: run git status to see which files have changed
3
Review changes: run git diff to inspect what exactly changed
4
Stage changes: run git add on the files you want in this commit
5
Commit with a message: run git commit -m "Your descriptive message"
6
Verify: run git log --oneline to confirm your commit appears

Example 1: Adding a New Function

You just wrote a calculateAverage() function in stats.cpp. Here is the commit workflow:

# Check what changed
$ git status
# Output: modified: stats.cpp

# Stage the file
$ git add stats.cpp

# Commit with a clear message
$ git commit -m "Add calculateAverage function to stats module"

Example 2: Fixing a Bug

You discovered that your for loop in search.cpp runs one iteration too many, causing a segfault. You fixed the bounds and now need to commit:

# See exactly what you changed
$ git diff search.cpp
# Shows: i <= arr.size() changed to i < arr.size()

$ git add search.cpp
$ git commit -m "Fix off-by-one error in linear search loop"

Example 3: Multi-File Commit

You created a new FileManager class across a header and implementation file. Both files belong in one commit because they form a single logical unit:

$ git add FileManager.h FileManager.cpp

$ git commit -m "Add FileManager class for reading/writing CSV data"

# Verify commit in history
$ git log --oneline -3
# a3f7b2c Add FileManager class for reading/writing CSV data
# b5e9a1f Fix off-by-one error in linear search loop
# c8d2e4a Add calculateAverage function to stats module

Reading the Commit History

a3f7b2c Add FileManager class for CSV data 2 min ago b5e9a1f Fix off-by-one error in search loop 15 min ago c8d2e4a Add calculateAverage to stats module 45 min ago d1f3a8b Initialize project with main.cpp skeleton 1 hour ago

Common Mistakes

✕ Vague Messages
Writing "fixed bug" or "updated code" tells nobody what actually changed. Be specific.
✕ Giant Commits
Committing 500 lines across 12 files at once. Break work into small, logical units.
✕ Forgetting to Stage
Running git commit without git add first. The commit will be empty or miss files.
✕ Committing Binaries
Including compiled files (a.out, .exe) in commits. Use a .gitignore file to exclude them.

Command Reference

Command Purpose When to Use
git status See changed files Before every commit
git diff See exact line changes To review before staging
git add <file> Stage specific files After editing files
git add . Stage all changes When all changes are related
git commit -m "" Create a commit After staging changes
git log --oneline View commit history To verify or browse history

Question 1 of 5

What does the git add command do?

Question 2 of 5

Which is the best commit message for fixing a segmentation fault in a sorting function?

Question 3 of 5

You edited both main.cpp and utils.h but only want to commit changes to utils.h. What do you do?

Question 4 of 5

What are the three areas that Git uses to manage your code, in order?

Question 5 of 5

You just added a new Player class in Player.h and Player.cpp. Which approach follows best practices?