Git warns about trailing whitespace when applying patches that add spaces or tabs at the end of lines. This is a code quality warning that can be fixed automatically or suppressed through configuration.
The "trailing whitespace" warning in Git appears when you apply a patch that introduces whitespace characters (spaces or tabs) at the end of lines. Git considers trailing whitespace problematic because it serves no purpose, can cause merge conflicts, and is generally considered poor code hygiene. When Git displays "warning: trailing whitespace in line X," it's telling you that line X in the patch adds trailing whitespace to the file. By default, Git still applies the patch despite the warning, but it's alerting you to a potential code quality issue. Git checks for three types of whitespace errors by default: - **blank-at-eol**: Spaces or tabs at the end of a line - **blank-at-eof**: Blank lines at the end of a file - **space-before-tab**: A space character before a tab in the initial indent This warning commonly appears when applying patches from different operating systems (especially Windows with CRLF line endings), when patches were created from files edited with misconfigured text editors, or when working with code generated by tools that don't strip trailing whitespace.
Use the --whitespace=fix option to automatically remove trailing whitespace while applying the patch:
git apply --whitespace=fix your-patch-file.patchThis applies the patch and strips any trailing whitespace, resolving the warning automatically. The fix option outputs warnings but applies the patch with whitespace corrected.
Set the apply.whitespace configuration to automatically fix whitespace for all future patch applications:
# Set for the current repository
git config apply.whitespace fix
# Set globally for all repositories
git config --global apply.whitespace fixThis makes --whitespace=fix the default behavior for git apply.
If trailing whitespace is intentional or you don't want to see warnings, suppress them:
# Suppress for a single command
git apply --whitespace=nowarn your-patch-file.patch
# Set as default configuration
git config --global apply.whitespace nowarnNote: This hides the warning but does not fix the whitespace in your files.
When applying patches from email or formatted patch files, use git am with the whitespace fix option:
git am --whitespace=fix your-patch-file.patchThis applies the patch as a commit and fixes whitespace issues automatically.
If you control the source, remove trailing whitespace before creating the patch:
# Remove trailing whitespace from all lines in a file
sed -i 's/[[:space:]]*$//' filename.txt
# On macOS, use:
sed -i '' 's/[[:space:]]*$//' filename.txtThen recreate the patch from the cleaned files.
If the patch contains Windows line endings (CRLF, shown as ^M), convert them:
# Using dos2unix
dos2unix your-patch-file.patch
# Or using sed
sed -i 's/\r$//' your-patch-file.patch
# On macOS
sed -i '' 's/\r$//' your-patch-file.patchThen apply the converted patch normally.
Prevent future issues by configuring your text editor:
VS Code (settings.json):
{
"files.trimTrailingWhitespace": true
}Vim (.vimrc):
autocmd BufWritePre * :%s/\s\+$//eSublime Text (Preferences):
{
"trim_trailing_white_space_on_save": true
}Add a Git pre-commit hook to prevent committing files with trailing whitespace:
# Create or edit .git/hooks/pre-commit
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
if git diff --check --cached | grep -q "trailing whitespace"; then
echo "Error: Trailing whitespace detected. Please fix before committing."
git diff --check --cached
exit 1
fi
EOF
chmod +x .git/hooks/pre-commitThis hook prevents commits that would introduce trailing whitespace.
### Understanding --whitespace Options
Git provides several whitespace handling modes for git apply:
| Option | Behavior |
|--------|----------|
| nowarn | Suppresses all whitespace warnings |
| warn | Outputs warnings but applies the patch as-is (default) |
| fix | Outputs warnings and fixes whitespace errors |
| error | Outputs warnings and refuses to apply the patch |
| error-all | Like error but shows all errors, not just the first few |
### Configuring core.whitespace
The core.whitespace setting controls which whitespace problems Git detects:
# View current settings
git config core.whitespace
# Enable all common checks
git config core.whitespace trailing-space,space-before-tab,blank-at-eof
# Disable a specific check (prefix with -)
git config core.whitespace trailing-space,-space-before-tabAvailable checks:
- blank-at-eol / trailing-space: Spaces at line ends (enabled by default)
- blank-at-eof: Blank lines at file end (enabled by default)
- space-before-tab: Space before tab in indentation (enabled by default)
- indent-with-non-tab: Spaces instead of tabs for indentation (disabled)
- tab-in-indent: Tabs in indentation (disabled)
- cr-at-eol: Carriage return at line end is OK (disabled)
### Using with git rebase
When rebasing, you can fix whitespace across multiple commits:
git rebase --whitespace=fix HEAD~5This replays the last 5 commits while fixing whitespace errors in each.
### The --ignore-whitespace Option
For cases where context lines have whitespace differences but you still want to apply:
git apply --ignore-whitespace your-patch-file.patchThis makes context matching more lenient but still respects the --whitespace setting for new lines.
warning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server