Git: Diff per word or character
By default git diff
highlights whole lines as changes.
To diff on a word-by-word basis you can say:
git diff --color-words
To diff on a character-by-character basis you can say:
git diff --color-words=.
Related cards:
How to fix: "git diff" does not show umlauts (or other non-ASCII characters) properly
When using git diff
, you might encounter weird characters where umlauts (or any other UTF-8) characters should be. It looks like this:
R<C3><BC>ckg<C3><A4>ngig # should be "Rückgängig"
However, not Git is to blame [but the less
comma...
Git: Diff changes in a long line efficiently
Instead of showing you two lines for each change, Git allows you to highlight changes in a line explicitly:
git diff --word-diff some_file
Hello [-world-]{+universe+}!
(The result is usually colored nicely, the removed part being red...
How to not leave trailing whitespace (using your editor or Git)
There is no reason to leave trailing whitespace characters in your project's files, so don't add any.
A git diff --check
will tell you if there are any and you should not commit when you see them. So go ahead and switch your editor/IDE to autom...
Git diff a file with another revision (or branch)
git diff commit_hash -- path/to/file
Provide any commit hashes or branch names like "master
" for commit_hash
.
Git: See more context in a diff
Using the -U
parameter you can change how many lines are shown above and below a changed section.
E.g. git diff -U10
will show 10 lines above and below.
Git: How to get a useful diff when renaming files
tldr; Use git diff -M
or git diff --find-renames
when you've moved a few files around.
Usage
$ git diff --help
Options:
-M[<n>], --find-renames[=<n>]
Detect renames. If n is specified, it is a threshold on the similarity ...
Git: Parsing large diffs as a human
I just finished migrating a project from the Asset Pipeline to Webpacker, this is what my diff to master looks like:
5.825 files changed, 44.805 insertio...
Git: Diff staged changes
Saying git diff
only shows unstaged changes relative to the index (or HEAD
if the index is empty, alternatively any hash or branch you supplied) but leaves out files you already staged for the next commit.
To diff your added changes with `HEA...
Run all RSpec tests edited or added in the current branch
With this command you can run all the spec files which have been edited or added in the current branch since master:
git diff --name-only master -- ./spec | xargs -I{} rspec {}
- If you have several spec folders add them for path p...
Git: When committing, check the diff
When committing, you should always check the diff of your changes so you don't include any leftovers or irrelevant/bad changes. Any time spent on that is well invested as it saves you rejected merge requests or any mess you need to clean up later....