01 / 22

Using .gitignore

Learn how to exclude build directories and binaries from your Git repository, keeping your C++ projects clean and efficient.

C++ Git Programming

What Is .gitignore?

A .gitignore file is a plain text file placed at the root of your Git repository. It tells Git which files and directories to intentionally ignore: they won't be tracked, staged, or committed.

Why Does It Matter for C++?

Bloated Repos
Compiled binaries and object files can be megabytes. Without .gitignore, every clone downloads unnecessary data.
Merge Conflicts
Binary files can't be merged. Committing them causes painful, unresolvable conflicts between teammates.
Clean History
Your commit log stays focused on real code changes, not noise from recompiled output files.

What C++ Projects Generate

Source Code main.cpp utils.h / utils.cpp Makefile / CMakeLists.txt compile Build Artifacts main.o, utils.o myapp (executable) build/ directory .gitignore *.o build/ myapp ✓ Track these ✕ Ignore these ✓ The filter Source code goes into Git. Generated files stay out.

The Core Rule

This principle applies beyond C++: Java's .class files, Python's __pycache__, and JavaScript's node_modules all follow the same logic.

Pattern Syntax

* Wildcard
Matches any number of characters within a filename: *.o ignores all .o files.
/ Directory
A trailing slash targets directories only: build/ ignores the entire build folder.
! Negation
An exclamation mark un-ignores a file: !important.o keeps that one file tracked.
# Comment
Lines starting with # are comments: use them to organize and document your rules.

Tracked vs Untracked vs Ignored

Tracked Untracked Ignored
Git knows about it? Yes No No
Shows in git status? If modified Yes No
Included in commits? When staged If you add it Never
Example main.cpp new_file.cpp main.o

Creating Your .gitignore

1
Create the file in your project root: name it exactly .gitignore (note the leading dot).
2
Add patterns for files to ignore: one pattern per line. Start with *.o and build/ for C++ projects.
3
Use comments with # to group rules by category: compiler output, IDE files, OS files.
4
Commit the .gitignore file itself. It should be tracked so all collaborators share the same ignore rules.
5
Verify by running git status: ignored files should no longer appear in the output.

Example: Basic .gitignore

# Compiled object files *.o *.obj # Executables *.exe *.out myapp # Build directory build/

Example: CMake Project

# CMake build output build/ cmake-build-*/ CMakeCache.txt CMakeFiles/ cmake_install.cmake Makefile # Compiled files *.o *.a *.so # IDE files .vscode/ .idea/ *.swp

Example: Negation Pattern

# Ignore all log files *.log # But keep the important one !error.log # Ignore all of build/ build/ # But keep the config inside it !build/config.json

How Patterns Are Matched

PROJECT ROOT .gitignore main.cpp utils.cpp main.o matched by *.o utils.o matched by *.o myapp matched by myapp build/ matched by build/ CMakeCache.txt myapp .gitignore CONTENTS # Object files *.o # Executable myapp # Build directory build/ Tracked by Git Ignored by Git

Global vs Local .gitignore

Local .gitignore Global .gitignore
Location Project root Home directory
Scope One repository All repositories
Shared with team? Yes, committed No, personal
Best for Build artifacts, project output OS files, IDE settings
Example entries *.o, build/ .DS_Store, Thumbs.db

Advanced Pattern: ** Double Star

The ** pattern matches across any number of directories. It's powerful for ignoring files no matter where they appear in the project tree.

# Ignore .o files anywhere in the tree **/*.o # Ignore any build/ directory at any depth **/build/ # Ignore all .log files inside logs/ subdirs logs/**/*.log

Already Tracked Files

Adding a pattern to .gitignore does not remove files that are already tracked. If you accidentally committed a build artifact, you need an extra step.

1
Add the pattern to .gitignore (e.g., build/).
2
Remove from tracking with git rm --cached <file>. The --cached flag removes it from Git's index without deleting the local file.
3
Commit the changes. The file is now untracked and ignored going forward.

Common Mistakes

✕ Forgetting the leading dot
The file must be named .gitignore, not gitignore. Without the dot, Git won't recognize it.
✕ Ignoring after committing
Adding a rule after a file is already tracked does nothing. You must also run git rm --cached.
✕ Negating inside an ignored dir
You cannot un-ignore a file inside an ignored directory. Git never looks inside a fully ignored folder.

C++ .gitignore Cheat Sheet

Pattern What It Ignores Category
*.o Object files from compilation Compiler
*.exe / *.out Executable binaries Linker
build/ Entire build directory Build System
*.a / *.so Static and shared libraries Libraries
.vscode/ VS Code settings folder IDE
*.swp Vim swap files Editor

Question 1 of 5

What is the primary purpose of a .gitignore file?

Question 2 of 5

Which pattern would correctly ignore all object files (files ending in .o) in a C++ project?

Question 3 of 5

You accidentally committed a build/ directory. You then add build/ to .gitignore. What happens?

Question 4 of 5

What does the ! prefix do in a .gitignore pattern?

Question 5 of 5

Which of these files should NOT typically be listed in a C++ project's .gitignore?