| 1 |
# Git Configuration File Format |
| 2 |
|
| 3 |
This document describes the Git configuration file format as implemented by this library, based on the [official Git documentation](https://git-scm.com/docs/git-config#_configuration_file). |
| 4 |
|
| 5 |
## File Structure |
| 6 |
|
| 7 |
A Git config file consists of sections and variables: |
| 8 |
|
| 9 |
```ini |
| 10 |
# Comment |
| 11 |
[section] |
| 12 |
key = value |
| 13 |
|
| 14 |
[section "subsection"] |
| 15 |
key = value |
| 16 |
``` |
| 17 |
|
| 18 |
## Syntax Rules |
| 19 |
|
| 20 |
### Sections |
| 21 |
|
| 22 |
Sections are defined using square brackets: |
| 23 |
|
| 24 |
```ini |
| 25 |
[core] |
| 26 |
[remote "origin"] |
| 27 |
[branch "main"] |
| 28 |
``` |
| 29 |
|
| 30 |
- **Simple section**: `[section]` |
| 31 |
- **Subsection**: `[section "subsection"]` |
| 32 |
- Section names are case-insensitive |
| 33 |
- Subsection names are case-sensitive |
| 34 |
|
| 35 |
### Keys |
| 36 |
|
| 37 |
Keys appear within sections: |
| 38 |
|
| 39 |
```ini |
| 40 |
[user] |
| 41 |
name = John Doe |
| 42 |
email = [email protected] |
| 43 |
``` |
| 44 |
|
| 45 |
- Key names are case-insensitive |
| 46 |
- Keys can contain alphanumeric characters and dashes |
| 47 |
- Whitespace before `=` is ignored |
| 48 |
- Keys can appear multiple times (multivars) |
| 49 |
|
| 50 |
### Values |
| 51 |
|
| 52 |
Values follow the `=` sign: |
| 53 |
|
| 54 |
```ini |
| 55 |
[core] |
| 56 |
editor = vim |
| 57 |
autocrlf = true |
| 58 |
excludesfile = ~/.gitignore_global |
| 59 |
``` |
| 60 |
|
| 61 |
**Value types:** |
| 62 |
|
| 63 |
- **String**: Any text (quotes optional unless special characters present) |
| 64 |
- **Boolean**: `true`, `false`, `yes`, `no`, `on`, `off`, `1`, `0` |
| 65 |
- **Integer**: Numeric value (parsed as string by this library) |
| 66 |
|
| 67 |
**Special characters in values:** |
| 68 |
|
| 69 |
```ini |
| 70 |
[section] |
| 71 |
# Quoted string with spaces |
| 72 |
key = "value with spaces" |
| 73 |
|
| 74 |
# Escaped characters |
| 75 |
path = "C:\\Users\\Name\\Path" |
| 76 |
|
| 77 |
# Empty value |
| 78 |
emptykey = |
| 79 |
``` |
| 80 |
|
| 81 |
### Escape Sequences |
| 82 |
|
| 83 |
Within double-quoted strings: |
| 84 |
|
| 85 |
| Sequence | Meaning | |
| 86 |
|----------|---------| |
| 87 |
| `\\` | Backslash | |
| 88 |
| `\"` | Double quote | |
| 89 |
| `\n` | Newline | |
| 90 |
| `\t` | Tab | |
| 91 |
| `\b` | Backspace | |
| 92 |
|
| 93 |
Example: |
| 94 |
|
| 95 |
```ini |
| 96 |
[alias] |
| 97 |
log1 = "log --pretty=format:\"%h %s\"" |
| 98 |
``` |
| 99 |
|
| 100 |
### Comments |
| 101 |
|
| 102 |
Comments start with `#` or `;`: |
| 103 |
|
| 104 |
```ini |
| 105 |
# This is a comment |
| 106 |
; This is also a comment |
| 107 |
|
| 108 |
[section] |
| 109 |
key = value # Inline comments are NOT standard Git behavior |
| 110 |
``` |
| 111 |
|
| 112 |
**Note**: This library may not handle inline comments correctly. Use full-line comments. |
| 113 |
|
| 114 |
### Whitespace |
| 115 |
|
| 116 |
- Leading and trailing whitespace in values is trimmed |
| 117 |
- Internal whitespace in unquoted values is preserved |
| 118 |
- Use quotes to preserve leading/trailing whitespace |
| 119 |
|
| 120 |
```ini |
| 121 |
[section] |
| 122 |
# These are equivalent: |
| 123 |
key1 = value |
| 124 |
key1=value |
| 125 |
key1 = value |
| 126 |
|
| 127 |
# These preserve whitespace: |
| 128 |
key2 = " value with spaces " |
| 129 |
``` |
| 130 |
|
| 131 |
## Multi-valued Keys |
| 132 |
|
| 133 |
Some keys can have multiple values: |
| 134 |
|
| 135 |
```ini |
| 136 |
[remote "origin"] |
| 137 |
fetch = +refs/heads/*:refs/remotes/origin/* |
| 138 |
fetch = +refs/pull/*/head:refs/remotes/origin/pr/* |
| 139 |
``` |
| 140 |
|
| 141 |
Access with: |
| 142 |
|
| 143 |
```go |
| 144 |
values, ok := cfg.GetAll("remote.origin.fetch") |
| 145 |
// values = ["+refs/heads/*...", "+refs/pull/*..."] |
| 146 |
``` |
| 147 |
|
| 148 |
## Include Directives |
| 149 |
|
| 150 |
### Basic Includes |
| 151 |
|
| 152 |
Include other config files: |
| 153 |
|
| 154 |
```ini |
| 155 |
[include] |
| 156 |
path = /path/to/other.gitconfig |
| 157 |
path = ~/.gitconfig-extras |
| 158 |
``` |
| 159 |
|
| 160 |
**Path resolution:** |
| 161 |
|
| 162 |
- Relative paths are resolved from the directory of the current config file |
| 163 |
- `~` expands to user home directory |
| 164 |
- Absolute paths work as expected |
| 165 |
|
| 166 |
### Conditional Includes |
| 167 |
|
| 168 |
Include files based on conditions: |
| 169 |
|
| 170 |
```ini |
| 171 |
[includeIf "gitdir:~/work/"] |
| 172 |
path = ~/.gitconfig-work |
| 173 |
|
| 174 |
[includeIf "gitdir/i:C:/projects/"] |
| 175 |
path = ~/gitconfig-windows |
| 176 |
``` |
| 177 |
|
| 178 |
**Supported conditions:** |
| 179 |
|
| 180 |
- `gitdir:<pattern>` - Include if git directory matches pattern (case-sensitive) |
| 181 |
- `gitdir/i:<pattern>` - Include if git directory matches pattern (case-insensitive) |
| 182 |
- `onbranch:<pattern>` - Include if operating on a specific branch |
| 183 |
|
| 184 |
**Current limitations:** |
| 185 |
|
| 186 |
- `hasconfig:remote.*.url:<pattern>` - Not supported |
| 187 |
|
| 188 |
### Include Precedence |
| 189 |
|
| 190 |
Later includes override earlier ones: |
| 191 |
|
| 192 |
```ini |
| 193 |
[user] |
| 194 |
email = [email protected] |
| 195 |
|
| 196 |
[include] |
| 197 |
path = ~/.gitconfig-work # May override user.email |
| 198 |
``` |
| 199 |
|
| 200 |
Settings in included files follow normal override rules. |
| 201 |
|
| 202 |
## Key Naming Conventions |
| 203 |
|
| 204 |
### Section Hierarchy |
| 205 |
|
| 206 |
Keys use dot notation: |
| 207 |
|
| 208 |
``` |
| 209 |
section.key |
| 210 |
section.subsection.key |
| 211 |
``` |
| 212 |
|
| 213 |
Examples: |
| 214 |
|
| 215 |
```ini |
| 216 |
[core] |
| 217 |
editor = vim |
| 218 |
# Accessed as: core.editor |
| 219 |
|
| 220 |
[remote "origin"] |
| 221 |
url = https://github.com/user/repo.git |
| 222 |
# Accessed as: remote.origin.url |
| 223 |
|
| 224 |
[branch "main"] |
| 225 |
remote = origin |
| 226 |
# Accessed as: branch.main.remote |
| 227 |
``` |
| 228 |
|
| 229 |
### Case Sensitivity |
| 230 |
|
| 231 |
- **Section names**: Case-insensitive (`[Core]` = `[core]`) |
| 232 |
- **Subsection names**: Case-sensitive (`[remote "Origin"]` ≠ `[remote "origin"]`) |
| 233 |
- **Key names**: Case-insensitive (`Name` = `name`) |
| 234 |
|
| 235 |
## Common Patterns |
| 236 |
|
| 237 |
### User Information |
| 238 |
|
| 239 |
```ini |
| 240 |
[user] |
| 241 |
name = Jane Doe |
| 242 |
email = [email protected] |
| 243 |
signingkey = ABC123 |
| 244 |
``` |
| 245 |
|
| 246 |
### Core Settings |
| 247 |
|
| 248 |
```ini |
| 249 |
[core] |
| 250 |
editor = vim |
| 251 |
autocrlf = input |
| 252 |
filemode = true |
| 253 |
ignorecase = false |
| 254 |
quotepath = false |
| 255 |
``` |
| 256 |
|
| 257 |
### Remote Repositories |
| 258 |
|
| 259 |
```ini |
| 260 |
[remote "origin"] |
| 261 |
url = https://github.com/user/repo.git |
| 262 |
fetch = +refs/heads/*:refs/remotes/origin/* |
| 263 |
pushurl = [email protected]:user/repo.git |
| 264 |
|
| 265 |
[remote "upstream"] |
| 266 |
url = https://github.com/project/repo.git |
| 267 |
fetch = +refs/heads/*:refs/remotes/upstream/* |
| 268 |
``` |
| 269 |
|
| 270 |
### Branch Configuration |
| 271 |
|
| 272 |
```ini |
| 273 |
[branch "main"] |
| 274 |
remote = origin |
| 275 |
merge = refs/heads/main |
| 276 |
rebase = true |
| 277 |
|
| 278 |
[branch "develop"] |
| 279 |
remote = origin |
| 280 |
merge = refs/heads/develop |
| 281 |
``` |
| 282 |
|
| 283 |
### Aliases |
| 284 |
|
| 285 |
```ini |
| 286 |
[alias] |
| 287 |
st = status |
| 288 |
co = checkout |
| 289 |
br = branch |
| 290 |
ci = commit |
| 291 |
unstage = reset HEAD -- |
| 292 |
last = log -1 HEAD |
| 293 |
visual = log --graph --oneline --decorate --all |
| 294 |
``` |
| 295 |
|
| 296 |
### URL Rewrites |
| 297 |
|
| 298 |
```ini |
| 299 |
[url "[email protected]:"] |
| 300 |
insteadOf = https://github.com/ |
| 301 |
``` |
| 302 |
|
| 303 |
**Note**: URL rewrites are not fully supported by this library. |
| 304 |
|
| 305 |
## Configuration Scopes |
| 306 |
|
| 307 |
Git supports multiple configuration scopes with defined precedence: |
| 308 |
|
| 309 |
### Scope Hierarchy (Highest to Lowest) |
| 310 |
|
| 311 |
1. **Environment variables** - `GIT_CONFIG_COUNT`, `GIT_CONFIG_KEY_*`, `GIT_CONFIG_VALUE_*` |
| 312 |
2. **Worktree** - `.git/config.worktree` |
| 313 |
3. **Local** - `.git/config` (repository-specific) |
| 314 |
4. **Global** - `~/.gitconfig` or `$XDG_CONFIG_HOME/git/config` (user-specific) |
| 315 |
5. **System** - `/etc/gitconfig` (system-wide) |
| 316 |
|
| 317 |
### Scope File Locations |
| 318 |
|
| 319 |
Default locations by scope: |
| 320 |
|
| 321 |
| Scope | Linux/macOS | Windows | Customizable | |
| 322 |
|-------|-------------|---------|--------------| |
| 323 |
| System | `/etc/gitconfig` | `C:\ProgramData\Git\config` | Yes | |
| 324 |
| Global | `~/.gitconfig` | `C:\Users\<user>\.gitconfig` | Yes | |
| 325 |
| Local | `.git/config` | `.git\config` | No | |
| 326 |
| Worktree | `.git/config.worktree` | `.git\config.worktree` | No | |
| 327 |
|
| 328 |
## Library-Specific Behavior |
| 329 |
|
| 330 |
### Round-Trip Preservation |
| 331 |
|
| 332 |
This library attempts to preserve the original file structure when writing: |
| 333 |
|
| 334 |
**Preserved:** |
| 335 |
|
| 336 |
- Comments (full-line) |
| 337 |
- Blank lines |
| 338 |
- Section order |
| 339 |
- Key order within sections |
| 340 |
|
| 341 |
**Not Always Preserved:** |
| 342 |
|
| 343 |
- Exact whitespace formatting (tabs vs spaces) |
| 344 |
- Inline comments |
| 345 |
- Specific indentation |
| 346 |
|
| 347 |
### Limitations |
| 348 |
|
| 349 |
**Not Supported:** |
| 350 |
|
| 351 |
- Bare boolean values (keys without `=` sign) |
| 352 |
- Some advanced include conditions (onbranch, hasconfig) |
| 353 |
- URL rewrite patterns |
| 354 |
- Replacing specific instances of multivars |
| 355 |
|
| 356 |
**Partial Support:** |
| 357 |
|
| 358 |
- Worktree configurations |
| 359 |
- Some escape sequences |
| 360 |
- Inline comments |
| 361 |
|
| 362 |
### Differences from Git |
| 363 |
|
| 364 |
- **Error handling**: This library may be more lenient with malformed configs |
| 365 |
- **Whitespace**: Minor differences in whitespace preservation |
| 366 |
- **Extensions**: Git supports more conditional include types |
| 367 |
- **URL matching**: Git's URL insteadOf patterns are not implemented |
| 368 |
|
| 369 |
## Validation and Error Handling |
| 370 |
|
| 371 |
### Valid Configuration |
| 372 |
|
| 373 |
```ini |
| 374 |
[section] |
| 375 |
key = value |
| 376 |
``` |
| 377 |
|
| 378 |
### Common Errors |
| 379 |
|
| 380 |
**1. Missing section header:** |
| 381 |
|
| 382 |
```ini |
| 383 |
# ERROR: key without section |
| 384 |
key = value |
| 385 |
``` |
| 386 |
|
| 387 |
**2. Invalid section syntax:** |
| 388 |
|
| 389 |
```ini |
| 390 |
# ERROR: unmatched quotes |
| 391 |
[section "subsection] |
| 392 |
``` |
| 393 |
|
| 394 |
**3. Circular includes:** |
| 395 |
|
| 396 |
```ini |
| 397 |
# config-a |
| 398 |
[include] |
| 399 |
path = config-b |
| 400 |
|
| 401 |
# config-b |
| 402 |
[include] |
| 403 |
path = config-a # ERROR: circular reference |
| 404 |
``` |
| 405 |
|
| 406 |
**4. Invalid escape sequences:** |
| 407 |
|
| 408 |
```ini |
| 409 |
[section] |
| 410 |
# ERROR: unknown escape sequence |
| 411 |
key = "value\x" |
| 412 |
``` |
| 413 |
|
| 414 |
## Best Practices |
| 415 |
|
| 416 |
1. **Use appropriate scopes** |
| 417 |
- User preferences → Global (`~/.gitconfig`) |
| 418 |
- Repository settings → Local (`.git/config`) |
| 419 |
- System defaults → System (`/etc/gitconfig`) |
| 420 |
|
| 421 |
2. **Quote values with special characters** |
| 422 |
|
| 423 |
```ini |
| 424 |
[section] |
| 425 |
# Good |
| 426 |
path = "C:\\Users\\Name\\Documents" |
| 427 |
|
| 428 |
# May cause issues |
| 429 |
path = C:\Users\Name\Documents |
| 430 |
``` |
| 431 |
|
| 432 |
3. **Comment your configuration** |
| 433 |
|
| 434 |
```ini |
| 435 |
[core] |
| 436 |
# Use vim for commit messages |
| 437 |
editor = vim |
| 438 |
``` |
| 439 |
|
| 440 |
4. **Organize related settings** |
| 441 |
|
| 442 |
```ini |
| 443 |
[user] |
| 444 |
name = Jane Doe |
| 445 |
email = [email protected] |
| 446 |
|
| 447 |
[commit] |
| 448 |
gpgsign = true |
| 449 |
|
| 450 |
[gpg] |
| 451 |
program = gpg2 |
| 452 |
``` |
| 453 |
|
| 454 |
5. **Use includes for environment-specific settings** |
| 455 |
|
| 456 |
```ini |
| 457 |
# ~/.gitconfig |
| 458 |
[include] |
| 459 |
path = ~/.gitconfig-personal |
| 460 |
|
| 461 |
[includeIf "gitdir:~/work/"] |
| 462 |
path = ~/.gitconfig-work |
| 463 |
``` |
| 464 |
|
| 465 |
## Examples |
| 466 |
|
| 467 |
### Complete Configuration File |
| 468 |
|
| 469 |
```ini |
| 470 |
# Global Git configuration |
| 471 |
# ~/.gitconfig |
| 472 |
|
| 473 |
[user] |
| 474 |
name = Jane Doe |
| 475 |
email = [email protected] |
| 476 |
signingkey = ABC123DEF456 |
| 477 |
|
| 478 |
[core] |
| 479 |
editor = vim |
| 480 |
autocrlf = input |
| 481 |
excludesfile = ~/.gitignore_global |
| 482 |
|
| 483 |
[commit] |
| 484 |
gpgsign = true |
| 485 |
verbose = true |
| 486 |
|
| 487 |
[alias] |
| 488 |
st = status -sb |
| 489 |
co = checkout |
| 490 |
br = branch |
| 491 |
ci = commit |
| 492 |
unstage = reset HEAD -- |
| 493 |
last = log -1 HEAD |
| 494 |
lg = log --graph --pretty=format:'%h %s' |
| 495 |
|
| 496 |
[push] |
| 497 |
default = current |
| 498 |
followTags = true |
| 499 |
|
| 500 |
[pull] |
| 501 |
rebase = true |
| 502 |
|
| 503 |
[remote "origin"] |
| 504 |
fetch = +refs/heads/*:refs/remotes/origin/* |
| 505 |
|
| 506 |
[includeIf "gitdir:~/work/"] |
| 507 |
path = ~/.gitconfig-work |
| 508 |
``` |
| 509 |
|
| 510 |
### Environment-Specific Configuration |
| 511 |
|
| 512 |
**Personal** (`~/.gitconfig-personal`): |
| 513 |
|
| 514 |
```ini |
| 515 |
[user] |
| 516 |
email = [email protected] |
| 517 |
|
| 518 |
[core] |
| 519 |
editor = code --wait |
| 520 |
``` |
| 521 |
|
| 522 |
**Work** (`~/.gitconfig-work`): |
| 523 |
|
| 524 |
```ini |
| 525 |
[user] |
| 526 |
email = [email protected] |
| 527 |
signingkey = WORK123KEY456 |
| 528 |
|
| 529 |
[core] |
| 530 |
editor = vim |
| 531 |
|
| 532 |
[commit] |
| 533 |
gpgsign = true |
| 534 |
``` |
| 535 |
|
| 536 |
## References |
| 537 |
|
| 538 |
- **Official Git Documentation**: <https://git-scm.com/docs/git-config> |
| 539 |
- **Configuration File Format**: <https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html> |
| 540 |
- **Git Book - Configuration**: <https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration> |