0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

editorconfig vs clang-format vs gitattributes

0
Last updated at Posted at 2026-08-06

1. EditorConfig vs clang-format

比較項目 .editorconfig .clang-format
役割 ファイル保存ルールの統一 C/C++コードフォーマットの統一
読み取り元 エディタ(VS、VS Code、JetBrainsなど) clang-format、Visual Studio、VS Code、Clang Power Toolsなど
適用タイミング ファイルを保存する際 コードをフォーマットする際(Format Document、保存時の自動フォーマットなど)
言語依存性 いいえ、すべてのテキストファイルに適用可能 はい、主にC/C++, Objective-C, Java, JavaScriptなど、clang-formatをサポートする言語向け
コードレイアウト変更の有無 ❌ なし ✅ あり

EditorConfigは何を管理するのか?

✓ UTF-8 / Shift_JIS
✓ CRLF / LF
✓ Tab / Space
✓ インデント幅
✓ 行末の空白文字削除
✓ ファイル末尾の空行

例:

charset = utf-8
end_of_line = crlf
indent_style = space
indent_size = 4

clang-formatは何を管理するのか?

✓ 波括弧のスタイル
✓ if/forの改行
✓ 引数の改行
✓ アライメント
✓ includeのソート
✓ ポインタ *
✓ スペースのスタイル

例:

if (a)
{
    foo();
}

フォーマット後:

if (a) {
    foo();
}

まとめる

EditorConfigは「ファイルの保存方法」を、clang-formatは「コードのレイアウト」を管理します。

両者は相互補完的であり、衝突しません。


2. .gitattributes vs .editorconfig

比較項目 .editorconfig .gitattributes
役割 エディタの保存ルールの統一 Gitのファイル処理ルールの統一
読み取り元 エディタ Git
適用タイミング ファイルを保存する際 git add / checkout / clone / merge
Gitへの影響 ❌ なし ✅ あり
エディタへの影響 ✅ あり ❌ なし

EditorConfig

担当:

ファイルを保存
    ↓
UTF-8
CRLF
4 Spaces

例:

charset = utf-8
end_of_line = crlf
indent_style = space

GitAttributes

担当:

Git add
Git checkout
Git clone

例:

*.cpp text eol=crlf
*.sh  text eol=lf

Gitは以下のルールに基づいて決定します:

  • リポジトリに何を保存するか(LF / CRLF)
  • チェックアウト後の作業ディレクトリのフォーマット

ワークフロー

コードを記述
   │
   ▼
EditorConfig
(ファイルを保存)
   │
   ▼
作業ディレクトリのファイル
   │
git add
   ▼
GitAttributes
(改行コードを変換)
   │
   ▼
Gitリポジトリ

まとめる

EditorConfigは「エディタがファイルをどのように保存するか」を、.gitattributesは「Gitがファイルをどのように保存し、チェックアウトするか」を管理します。

これらも相互補完的であり、衝突しません。


推奨される組み合わせ(C++プロジェクト)

ファイル 推奨度 役割
.editorconfig ⭐⭐⭐⭐⭐ エンコーディング、改行、インデントなどの編集ルールを統一
.clang-format ⭐⭐⭐⭐⭐ コードフォーマットを統一
.gitattributes ⭐⭐⭐⭐⭐ Gitの改行コードとファイル属性を統一

これは、現在の多くのモダンなC++チーム(LLVM、Chromium、Qtなど)が採用している標準的な組み合わせです:EditorConfigは保存を、clang-formatはフォーマットを、.gitattributesはバージョン管理をそれぞれ担当します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?