Skip to content

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.

Terminal window
git clang-format [<options>] [--] [<file>...]
OptionDescription
-h, —helpDisplay help and exit
—versionDisplay version information
—diffShow diff for formatted files
—forceForce 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)
—quietSuppress progress and warnings
—verboseShow verbose output
-qSame as —quiet
ParameterDescription
Specific files to format (all changed files if not specified)
Terminal window
git clang-format

Formats all changed C/C++ files against HEAD.

Terminal window
git clang-format file1.cpp file2.h

Formats only the specified files.

Terminal window
git clang-format --commit origin/main

Formats changes since origin/main.

Terminal window
git clang-format --style=google

Applies Google style formatting.

Terminal window
git clang-format --diff

Displays what changes would be made without applying them.

Terminal window
git clang-format --binary /usr/local/bin/clang-format-11

Uses a specific clang-format executable.

Terminal window
git clang-format --exclude=third_party/

Skips formatting files under third_party directory.

To format all changed C/C++ files, use:

Terminal window
git clang-format

How 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:

Terminal window
git clang-format --diff

How do I specify a custom formatting style?

Section titled “How do I specify a custom formatting style?”

To specify a custom formatting style, execute:

Terminal window
git clang-format --style=<style>

To format specific files only, use:

Terminal window
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:

Terminal window
git clang-format --exclude=<path>

Applications of the git clang-format command

Section titled “Applications of the git clang-format command”
  1. Maintaining consistent code formatting across large C/C++ codebases
  2. Integrating automatic formatting into development workflows
  3. Reducing code review time by eliminating formatting discussions
  4. Enforcing corporate or project-specific coding standards
  5. Cleaning up legacy code with uniform formatting
  6. Preparing code for submission to upstream projects with specific style requirements