git-clean-whitespace
A script to remove whitespace-only changes from the staging area, leaving only non-whitespace changes staged for commit. Can also work in reverse to remove non-whitespace changes and leave only whitespace changes.
Overview
This script is the complement to git-clean-nonwhitespace. It removes whitespace-only changes from the staging area while preserving logical code changes. This is useful when you want to commit substantive changes without including incidental whitespace modifications.
With the --reverse option, it does the opposite: removes non-whitespace changes and leaves only whitespace changes staged. This provides the same functionality as git-clean-nonwhitespace.
You can process all staged files or target a specific file.
Usage
# Remove whitespace changes from all staged files
git clean-whitespace
# Remove NON-whitespace changes from all staged files (leave only whitespace)
git clean-whitespace --reverse
# Remove whitespace changes from a specific file (relative to current directory)
git clean-whitespace src/main.js
# Remove NON-whitespace changes from a specific file
git clean-whitespace --reverse src/main.js
# Remove whitespace changes from a file using absolute path
git clean-whitespace /full/path/to/project/src/main.js
# Remove whitespace changes from a file in parent directory
git clean-whitespace ../other-module/file.js
# Show help
git clean-whitespace --helpOptions
--reverse: Remove non-whitespace changes instead of whitespace changes (same asgit-clean-nonwhitespace)filename: Optional. Only process the specified file. Can be:- Relative to current working directory:
src/main.js - Absolute system path:
/full/path/to/file.js - Path relative to parent directories:
../other-file.js
- Relative to current working directory:
--help,-h: Show usage information
How It Works
The script:
- Always runs from git repository root to ensure git commands work properly
- Converts file paths from your current working directory or absolute paths to paths relative to the git repository root
- Identifies whitespace-only changes using
git diff --cached -w -R - Removes those changes from the staging area, leaving only meaningful code changes staged
When a filename is specified, the script:
- Converts the provided file path to be relative to the git repository root
- Checks if the file is actually staged for commit
- Processes only that file instead of all staged files
- Exits with an error if the specified file is not staged
Path conversion examples:
- Current directory:
src/main.js→packages/frontend/src/main.js(if running frompackages/frontend/) - Absolute path:
/home/user/project/lib/utils.js→lib/utils.js - Parent directory:
../shared/types.ts→shared/types.ts(if running frompackages/frontend/)
Use Case Examples
Process All Files
- You're working on a feature and your editor automatically fixes whitespace
- You make logical code changes along with these automatic whitespace fixes
- You stage all changes with
git add . - You want to commit only the logical changes, not the whitespace fixes
- Run
git clean-whitespaceto remove whitespace changes from staging
Process Specific File
- You've made changes to multiple files
- Only one file has unwanted whitespace changes mixed with real changes
- Run
git clean-whitespace src/problematic-file.jsto clean just that file - Other staged files remain unchanged
Requirements
- Git repository with staged changes
- Standard Unix utilities
Notes
- This script only affects the staging area (index), not your working directory
- After running this script, your working directory will still contain all changes
- The whitespace changes remain in your working directory and can be staged later if needed
- This is particularly useful for keeping commits focused on logical changes
- Works well in combination with automated code formatting tools