Git is great at comparing lines of code that have changed but not so good at individual words within a line or paragraph of text.
When diffing you can use git diff --word-diff
to get more granular word and punctuation changes but you won’t see white space changes.
Use git diff --word-diff-regex=.
to see all changes at a more granular level.
You can see the changes that way but I’m yet to find an easy way to split out individual changes on the same line into separate commits. Patch mode doesn’t work for this (it’s still whole lines at a time).
My goal was to take a fairly long document that I’d made a number of different changes to (punctuation, grammar, spelling mistakes, wording changes, whitespace errors, etc) and commit the changes by type… one commit for all the spelling mistakes, one for all the whitespace changes and separate ones for each of the suggested wording changes. The reason for this is to allow the different commits to be accepted or rejected individually.
It gets very tricky when one line has multiple changes… a typo, some whitespace errors and a suggested change. I want each of those in different commits but I can’t easily just grab one word of the line.
So far, the best method I have found (e.g. to just fix the spelling mistakes) is to use git diff --word-diff-regex=.
in one terminal window to see the individual changes, copy a whole line in my editor, discard the changes to that line, fix the spelling mistake again, save and stage that line, then paste the whole line back in with its other changes and save again.
The end result is the spelling fix is staged and the other changes to that line are saved in my working tree. Once I’ve repeated that process for all the spelling mistakes I’ll be able to commit and move on to one of the other fixes.
Overall I’m not happy with this technique so I’m going to research other options tomorrow.