clang-format Git Command Guide
The git clang-format command runs the clang-format tool on the changed regions of files in a Git repository, helping maintain consistent code formatting in C/C++ projects.
git clang-format Syntax:
Section titled “git clang-format Syntax:”git clang-format [<options>] [--] [<file>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| -h, —help | Display help and exit |
| —version | Display version information |
| —diff | Show diff for formatted files |
| —force | Force formatting even if clang-format is not configured |
| —commit | Specify commit for comparison (default HEAD) |
| —binary | Specify clang-format binary path |
| —style | Specify formatting style (llvm, google, chromium, mozilla, webkit, file, none) |
| —extensions | Comma-separated list of file extensions to format |
| —exclude | Exclude paths from formatting (repeatable) |
| —quiet | Suppress progress and warnings |
| —verbose | Show verbose output |
| -q | Same as —quiet |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| Specific files to format (all changed files if not specified) |
git clang-format Command Samples:
Section titled “git clang-format Command Samples:”Format changed files in working directory
Section titled “Format changed files in working directory”git clang-formatFormats all changed C/C++ files against HEAD.
Format specific files
Section titled “Format specific files”git clang-format file1.cpp file2.hFormats only the specified files.
Format against specific commit
Section titled “Format against specific commit”git clang-format --commit origin/mainFormats changes since origin/main.
Use specific formatting style
Section titled “Use specific formatting style”git clang-format --style=googleApplies Google style formatting.
Show diff without applying changes
Section titled “Show diff without applying changes”git clang-format --diffDisplays what changes would be made without applying them.
Format with custom clang-format binary
Section titled “Format with custom clang-format binary”git clang-format --binary /usr/local/bin/clang-format-11Uses a specific clang-format executable.
Exclude certain paths
Section titled “Exclude certain paths”git clang-format --exclude=third_party/Skips formatting files under third_party directory.
How do I format all changed C/C++ files?
Section titled “How do I format all changed C/C++ files?”To format all changed C/C++ files, use:
git clang-formatHow can I see what clang-format will change without applying it?
Section titled “How can I see what clang-format will change without applying it?”To see what clang-format will change without applying it, run:
git clang-format --diffHow do I specify a custom formatting style?
Section titled “How do I specify a custom formatting style?”To specify a custom formatting style, execute:
git clang-format --style=<style>How can I format specific files only?
Section titled “How can I format specific files only?”To format specific files only, use:
git clang-format <file1> <file2>How do I exclude directories from formatting?
Section titled “How do I exclude directories from formatting?”To exclude directories from formatting, run:
git clang-format --exclude=<path>Applications of the git clang-format command
Section titled “Applications of the git clang-format command”- Maintaining consistent code formatting across large C/C++ codebases
- Integrating automatic formatting into development workflows
- Reducing code review time by eliminating formatting discussions
- Enforcing corporate or project-specific coding standards
- Cleaning up legacy code with uniform formatting
- Preparing code for submission to upstream projects with specific style requirements