User Agents

Web browsers and everyday web tech, explained

Show Space and Tabs in Git Diff: How to Reveal Hidden Whitespace

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Git diff can hide whitespace changes like spaces and tabs by default, making it hard to spot unintended formatting issues in code. By configuring git to show space and tabs explicitly, you gain full visibility into whitespace modifications, which is essential for maintaining clean, consistent code. This guide walks through how to reveal these hidden characters in git diff output using built-in git settings and command-line options. No external tools are required—everything uses standard git functionality.

What You'll Need

  • A working installation of Git (version 1.7.2 or higher recommended)
  • Access to a terminal or command line
  • A git repository with files that contain whitespace changes (for testing)
  • Basic familiarity with git diff commands

Step-by-Step

  1. Open your terminal or command-line interface. Navigate to the root directory of your git repository using cd /path/to/your/repo.
  2. Run a basic git diff to see the current output. Notice that added or removed spaces or tabs may not be clearly visible, especially if they occur at the end of lines or in otherwise blank lines.
  3. To show whitespace changes more clearly, use the --ws-error-highlight option. Run: git diff --ws-error-highlight=all. This highlights whitespace errors such as trailing spaces, tabs in indentation, and mixed spaces and tabs.
  4. For even clearer visualization, configure git to mark spaces and tabs with visible symbols. Use: git diff --color-words --ws-error-highlight=all. This combines word-level coloring with whitespace highlighting for better context.
  5. To make spaces appear as dots (·) and tabs as arrows (→), set the core.pager option to use a highlighting tool like less with color support. First, ensure your pager supports ANSI colors by running: git config --global core.pager "less -R".
  6. Now, enable whitespace visualization globally with: git config --global core.whitespace "cr-at-eol,indent-with-non-tab,trailing-space,space-before-tab,tab-in-indent,blank-at-eol". This tells git to detect and report various whitespace issues.
  7. Test the configuration by making a change in a file—add extra spaces at the end of a line or insert a tab in a space-indented block. Stage the change with git add ., then run git diff --cached.
  8. Observe how git now highlights problematic whitespace. Trailing spaces may appear in red, and tabs in indented regions may be marked differently depending on your terminal’s color scheme.
  9. To view tabs specifically as visible characters, use an external diff tool or pipe output through cat -A or sed. For example: git diff | cat -A. This replaces spaces with nothing (they remain blank), tabs with ^I, and line endings with $.
  10. For a persistent solution, create a custom git alias. Run: git config --global alias.difftab "!git diff | cat -A". Now use git difftab anytime to see all whitespace characters clearly.
  11. To reverse any global settings later, use git config --unset --global core.whitespace or git config --unset --global alias.difftab.

Common Mistakes

One common mistake is assuming that git diff always shows whitespace changes by default. It does not—trailing spaces and mixed indentation are often invisible without explicit configuration. Another error is misconfiguring the core.pager setting, such as using less without the -R flag, which prevents color codes from displaying correctly. Users may also forget to stage changes before running git diff --cached, leading to confusion about what is being compared. Finally, relying solely on GUI tools without understanding the underlying git settings can result in missed whitespace issues if the tool doesn’t highlight them by default.

Tips

Use git diff --check to quickly list lines with whitespace errors without showing the full diff. This is ideal for pre-commit checks. Combine --ws-error-highlight=context,added,deleted to limit highlighting to relevant parts of the diff. For team projects, document your whitespace policy and share git configuration settings via a .gitconfig snippet. Consider integrating whitespace checks into your CI/CD pipeline using scripts that run git diff --check on pull requests. If you frequently switch between projects with different whitespace rules, use local (non-global) git configurations in specific repositories instead of global ones.

Troubleshooting

If whitespace is still not visible, first verify that your terminal supports colored output and that less -R is active. Test with echo "\033[31mRed Text\033[0m"—if it doesn’t appear red, your terminal may need configuration. If cat -A doesn’t show tabs as expected, ensure you’re piping the full diff output correctly. If git shows false positives for whitespace errors, review your core.whitespace settings—some values like cr-at-eol may be unnecessary on Unix-like systems. If a custom alias doesn’t work, check for syntax errors in the command and ensure the shell interprets the ! correctly. Restart your terminal after config changes if settings don’t take effect immediately.

Frequently Asked Questions

Does git diff show tabs by default?

No, git diff does not visually distinguish tabs from spaces by default. Tabs appear as regular whitespace unless configured otherwise.

How can I see trailing spaces in git diff?

Use git diff --ws-error-highlight=trailing-space or enable core.whitespace=trailing-space to highlight trailing spaces in red.

Can I make spaces appear as dots in the diff?

Git doesn’t natively render spaces as dots, but you can use cat -A in a pipeline to visualize line endings and tabs. Spaces remain blank, but context improves with surrounding markers.

Is there a way to prevent whitespace issues before committing?

Yes—run git diff --check before committing, or set up a pre-commit hook that blocks commits with whitespace errors.

Why are my whitespace settings not applying to all repositories?

If you used --global, settings should apply everywhere. If not, check for local repo configurations overriding them with git config --list --show-origin.

Can I customize which whitespace types git highlights?

Yes—use --ws-error-highlight with values like added, deleted, space-before-tab, and tab-in-indent to fine-tune what’s highlighted.