How to fix warning: trailing whitespace in line 5 in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 318 views

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.

Quick fix

Fixes warning: trailing whitespace in line 5

git apply --whitespace=fix your-patch-file.patch
  1. Apply the patch with automatic whitespace fixing
  2. Configure Git to always fix whitespace when applying patches
  3. Suppress the warning if whitespace is acceptable
  4. Use git am for email patches with whitespace fixing
git โ€” error
git apply --whitespace=fix your-patch-file.patch
warning: trailing whitespace in line 5

What this error means

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.

Typical symptoms

  • Git displays 'warning: trailing whitespace' when running git apply
  • The warning shows specific line numbers where trailing whitespace was introduced
  • git diff shows whitespace highlighted in red at the end of lines
  • Pre-commit hooks reject commits due to whitespace errors
  • git rebase operations show whitespace warnings during patch application

Common causes

  1. Patch was created from files containing trailing spaces or tabs at line ends
  2. Text editor or IDE is configured to not trim trailing whitespace
  3. Patch file was created on Windows and contains CRLF line endings (shown as ^M)
  4. Copy-pasting code from web pages or documents that include invisible trailing spaces
  5. Code generators or formatters that don't strip trailing whitespace
  6. Files were edited with mixed editor configurations in a team

How to fix "warning: trailing whitespace in line 5"

1Apply the patch with automatic whitespace fixing

Use the --whitespace=fix option to automatically remove trailing whitespace while applying the patch:

bash
git apply --whitespace=fix your-patch-file.patch

This applies the patch and strips any trailing whitespace, resolving the warning automatically. The fix option outputs warnings but applies the patch with whitespace corrected.

2Configure Git to always fix whitespace when applying patches

Set the apply.whitespace configuration to automatically fix whitespace for all future patch applications:

bash
# Set for the current repository
git config apply.whitespace fix

# Set globally for all repositories
git config --global apply.whitespace fix

This makes --whitespace=fix the default behavior for git apply.

3Suppress the warning if whitespace is acceptable

If trailing whitespace is intentional or you don't want to see warnings, suppress them:

bash
# Suppress for a single command
git apply --whitespace=nowarn your-patch-file.patch

# Set as default configuration
git config --global apply.whitespace nowarn

Note: This hides the warning but does not fix the whitespace in your files.

4Use git am for email patches with whitespace fixing

When applying patches from email or formatted patch files, use git am with the whitespace fix option:

bash
git am --whitespace=fix your-patch-file.patch

This applies the patch as a commit and fixes whitespace issues automatically.

5Fix the source files before creating the patch

If you control the source, remove trailing whitespace before creating the patch:

bash
# Remove trailing whitespace from all lines in a file
sed -i 's/[[:space:]]*$//' filename.txt

# On macOS, use:
sed -i '' 's/[[:space:]]*$//' filename.txt

Then recreate the patch from the cleaned files.

6Fix Windows line endings in the patch file

If the patch contains Windows line endings (CRLF, shown as ^M), convert them:

bash
# 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.patch

Then apply the converted patch normally.

7Configure your editor to trim trailing whitespace

Prevent future issues by configuring your text editor:

VS Code (settings.json):

json
{
  "files.trimTrailingWhitespace": true
}

Vim (.vimrc):

vim
autocmd BufWritePre * :%s/\s\+$//e

Sublime Text (Preferences):

json
{
  "trim_trailing_white_space_on_save": true
}
8Use a pre-commit hook to catch whitespace errors

Add a Git pre-commit hook to prevent committing files with trailing whitespace:

bash
# 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-commit

This hook prevents commits that would introduce trailing whitespace.

Advanced notes

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

bash
# 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-tab

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

bash
git rebase --whitespace=fix HEAD~5

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

bash
git apply --ignore-whitespace your-patch-file.patch

This makes context matching more lenient but still respects the --whitespace setting for new lines.

Frequently asked questions

What does "warning: trailing whitespace in line 5" mean?
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.
How do I fix "warning: trailing whitespace in line 5"?
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.
What causes "warning: trailing whitespace in line 5"?
Patch was created from files containing trailing spaces or tabs at line ends Text editor or IDE is configured to not trim trailing whitespace Patch file was created on Windows and contains CRLF line endings (shown as ^M)
Is the "warning: trailing whitespace in line 5" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading