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.
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.
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.
The Three Areas of Git
Anatomy of a Commit
The Commit Command
$ 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
git add vs git commit
How to Apply the Workflow
Example 1: Adding a New Function
You just wrote a calculateAverage() function in stats.cpp. Here is the commit workflow:
$ 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:
$ 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 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
Common Mistakes
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?