From 8dd9f86b809a14f2cb34266e4ecc8ac94b6976b1 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 17 Apr 2025 18:19:12 -0500 Subject: [PATCH] Add Highlight Small Change On Single Line as a Git TIL --- README.md | 3 +- git/highlight-small-change-on-single-line.md | 44 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 git/highlight-small-change-on-single-line.md diff --git a/README.md b/README.md index bde8529..f4233ca 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). -_1639 TILs and counting..._ +_1640 TILs and counting..._ See some of the other learning resources I work on: - [Get Started with Vimium](https://egghead.io/courses/get-started-with-vimium~3t5f7) @@ -349,6 +349,7 @@ If you've learned something here, support my efforts writing daily TILs by - [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) +- [Highlight Small Change On Single Line](git/highlight-small-change-on-single-line.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-small-change-on-single-line.md b/git/highlight-small-change-on-single-line.md new file mode 100644 index 0000000..1f9ad9c --- /dev/null +++ b/git/highlight-small-change-on-single-line.md @@ -0,0 +1,44 @@ +# Highlight Small Change On Single Line + +Sometimes a change gets made on a single, long line of text in a Git tracked +file. If it is a small, subtle change, then it can be hard to pick out when +looking at the diff. A standard diff will show a green line of text stacked on +a red line of text with no more granular information. + +There are two ways we can improve the diff output in these situations. + +The first is built-in to git. It is the `--word-diff` flag which will visually +isolate the portions of the line that have changed. + +```bash +git diff --word-diff README.md +``` + +Which will produce something like this: + +```diff +A collection of concise write-ups on small things I learn [-day to day-]{+day-to-day+} across a +``` + +The outgoing part is wrapped in `[-...-]` and the incoming part is wrapped in +`{+...+}`. + +The second (and my preference) is to use +[`delta`](https://github.com/dandavison/delta) as an external differ and pager +for git. + +```bash +git -c core.pager=delta diff README.md +``` + +I cannot visually demonstrate the difference in a standard code block. So I'll +describe it. We see a red and green line stacked on each other, but with muted +background colors. Then the specific characters that are different stand out +because they are highlighted with brighter red and green. I [posted a visual +here](https://bsky.app/profile/jbranchaud.bsky.social/post/3ln245orlxs2j). + +`delta` can also be added as a standard part of your config like I demonstrate +in [Better Diffs With Delta](git/better-diffs-with-delta.md). + +h/t to [Dillon Hafer's post on +`--word-diff`](https://til.hashrocket.com/posts/t994rwt3fg-finds-diffs-in-long-line)