# Git Diffs of Untracked Files

December 17th, 2021

## Diffs

TL;DR: I like the nice output from git diff better than the native diff command, but how do I convince git to let me do this for files which aren't source controlled?

I like to wrap this in a shell function I call fdiff.

Here's a more minimal version

## Explanation

Breaking this down, the most important pieces are

git
The git command.
diff
The diff command for git.
--no-index
This flag is the magic sauce that lets us use git diff with files that are not source controlled.
-- $1$2
These are just passing the filepaths down to git so it knows where to look.

Nice extra features.

--no-pager
Tells git to print the output to stdout instead of opening a paging program. Note: this flag must come before diff
--color=always
Preserves the diff colors when you pipe to other programs, e.g. grep which you may not want!
--src-prefix="old = "
--dst-prefix="new = "
Tells git to display the file names as "old" and "new" instead of "a" and "b". Very much a personal preference.
\${@:3}
Allows you to pass further arguments to git diff. See below for some examples.

You can read about all of the possible flags that can be passed to git diff by consulting git help diff.

## Example Usage

Here we'll use the following two files, old.txt and new.txt.

### Basic Usage

Running fdiff on these files produces output like this.

### Diffs on words

For day-to-day usage, I find --word-diff or --color-words the most useful.

### Summarizing Diffs

If you just want an overview of diffs, you can use flags like --compact-summary or --numstat

### Seeing Where Text Moved

If you're writing a longer document and you want to see where pieces have moved, you can use --color-moved=plain. Here I'm no longer using our previous example files.

### Context

You can specify the number of lines of context (lines before and after) you want with the -U flag. So for 0 lines of context, use -U0. For 10 lines, -U10, etc. I often use this in combination with --word-diff.