From db00ec69c2e5a570eaf2f4d01883751a43b34713 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 11 Dec 2024 18:43:08 -0600 Subject: [PATCH] Add Highlight Extra Whitespace In Diff Output as a Git TIL --- README.md | 3 +- ...ghlight-extra-whitespace-in-diff-output.md | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 git/highlight-extra-whitespace-in-diff-output.md diff --git a/README.md b/README.md index 976223b..789a3f1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1534 TILs and counting..._ +_1535 TILs and counting..._ --- @@ -317,6 +317,7 @@ _1534 TILs and counting..._ - [Grab A Single File From A Stash](git/grab-a-single-file-from-a-stash.md) - [Grep For A Pattern On Another Branch](git/grep-for-a-pattern-on-another-branch.md) - [Grep Over Commit Messages](git/grep-over-commit-messages.md) +- [Highlight Extra Whitespace In Diff Output](git/highlight-extra-whitespace-in-diff-output.md) - [Ignore Changes To A Tracked File](git/ignore-changes-to-a-tracked-file.md) - [Ignore Files Specific To Your Workflow](git/ignore-files-specific-to-your-workflow.md) - [Include A Message With Your Stashed Changes](git/include-a-message-with-your-stashed-changes.md) diff --git a/git/highlight-extra-whitespace-in-diff-output.md b/git/highlight-extra-whitespace-in-diff-output.md new file mode 100644 index 0000000..2fe529c --- /dev/null +++ b/git/highlight-extra-whitespace-in-diff-output.md @@ -0,0 +1,30 @@ +# Highlight Extra Whitespace In Diff Output + +When running a `git diff` (or `git add --patch`) I'll sometimes come across +lines that don't have any visible changes. This is usually because some +whitespace characters were either added (on accident) or removed (often by a +autoformatter). + +Depending on the `core.whitespace` config, you'll probably see at least some of +the whitespace errors that git provides. By default, git only highlights +whitespace errors on added (`new`) lines. However if some extra whitespace was +originally committed and is now being removed, it won't be highlighted on the +`old` line in the diff. + +We can have git always highlight whitespace errors by setting +`wsErrorHighlight` to `all` in the global git config. + +```bash +$ git config --global diff.wsErrorHighlight all +``` + +Which updates the global gitconfig file with the following line: + +``` +[diff] + wsErrorHighlight = all +``` + +The `all` option is a shorthand for `old,new,context`. + +See `man git-diff` for more details.