This error occurs when Git encounters a malformed or corrupted configuration file. The config file has invalid syntax, missing sections, special characters, or invisible formatting issues that prevent Git from parsing it.
When you see `fatal: bad config line X in file /path/to/.gitconfig`, Git is telling you that it found invalid syntax on a specific line in one of your configuration files. Git configuration files use an INI-like format with sections in square brackets followed by key-value pairs. The error typically occurs because the config file has been corrupted, edited incorrectly, or contains characters that Git cannot parse. Common culprits include incomplete section headers (missing closing brackets), incorrect indentation, special quote characters inserted by text editors like Mac TextEdit, lone backslashes, or invisible characters like NULL bytes or Windows carriage returns. Git reads configuration from three locations: system-wide (`/etc/gitconfig`), user-global (`~/.gitconfig` or `~/.config/git/config`), and repository-local (`.git/config`). The error message tells you exactly which file and line number contains the problem.
The error message tells you exactly where the problem is. Note the file path and line number:
fatal: bad config line 3 in file /home/user/.gitconfigIn this example, line 3 of ~/.gitconfig has the issue. Open the file to inspect:
# View the file with line numbers
cat -n ~/.gitconfig
# Or use your preferred editor
nano ~/.gitconfig
code ~/.gitconfigOften the problem is invisible characters that look fine but break parsing. Use cat -A to reveal hidden characters:
cat -A ~/.gitconfigLook for:
- ^M at line endings (Windows carriage returns)
- ^@ or M-^@ (NULL characters)
- Extra whitespace or tabs where there should not be any
- Non-ASCII characters in section names or keys
If the file appears empty but has content, or shows strange characters, that is likely the problem.
If you edited the config file with Mac TextEdit or certain other editors, they may have converted standard double quotes to curly 'smart' quotes:
Bad (curly quotes):
[user]
email = "[email protected]"Good (straight quotes):
[user]
email = "[email protected]"Open the file in a plain text editor (VS Code, nano, vim) and replace any curly quotes with standard ASCII quotes. In VS Code, use Find and Replace with:
- Find: [“”] (regex mode)
- Replace: "
Git config files must follow INI format with sections and key-value pairs. A common mistake is writing Git commands directly into the file:
Wrong:
git config core.editor "nano"Correct:
[core]
editor = nanoEnsure every section header has both opening [ and closing ] brackets, and values are on their own lines with key = value format.
If you have Windows paths in your config, backslashes need to be escaped or replaced:
Wrong:
[core]
editor = C:\Program Files\Notepad++\notepad++.exeCorrect (escaped backslashes):
[core]
editor = C:\\Program Files\\Notepad++\\notepad++.exeAlso correct (forward slashes):
[core]
editor = C:/Program Files/Notepad++/notepad++.exeOr use quotes with single backslashes:
[core]
editor = "C:\\Program Files\\Notepad++\\notepad++.exe"If you cannot spot the issue visually, comment out sections until Git works:
# Comment out all lines with # and test
git config --global --list
# If that works, uncomment sections one at a time
# to isolate which section causes the errorAlternatively, rename the config file and recreate it:
mv ~/.gitconfig ~/.gitconfig.backup
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Then compare the new working config with your backup to find differences.
If the file is severely corrupted, the fastest fix is to recreate it:
# Backup the old config
mv ~/.gitconfig ~/.gitconfig.old
# Create a fresh config with essential settings
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "code --wait"For repository-local config issues (.git/config):
# If no commits exist, reinitialize
rm -rf .git
git init
# If commits exist, copy config from another working repo
# or re-clone from remoteAfter making changes, verify Git can read the config:
# List all config values (should work without errors)
git config --global --list
# Check repository-specific config
git config --local --list
# Run a simple Git command
git statusIf these commands run without the 'bad config line' error, the issue is resolved.
Multiple config file locations: Git reads configuration from multiple files in order of precedence. If you fix one file but still get errors, check all three locations:
1. System: /etc/gitconfig (affects all users)
2. Global: ~/.gitconfig or ~/.config/git/config (user-specific)
3. Local: .git/config (repository-specific)
Symlink issues on Windows/Cygwin: If you use Cygwin with Windows Git, symlinks can cause problems. Cygwin creates symlinks that native Windows Git cannot read properly. The symlink file may contain binary data instead of the expected text content. Consider using hard links or copying the file instead.
Config includes: Git supports including other config files with [include] directives. If you use includes, the error might be in an included file rather than the main config:
[include]
path = ~/.gitconfig.localCheck included files as well when troubleshooting.
SSH vs Git config: Some users mistakenly put SSH configuration into .gitconfig. SSH config belongs in ~/.ssh/config, not in Git's configuration files.
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git