Bat: How to Use It with git diff

Updated

Learn how to use bat with git diff to highlight your changes in the terminal

Bat: How to Use It with git diff

Before we start, make sure you have bat and git installed on your computer, and a git repository with some changes in it. If you don't have bat yet, you can install it with Homebrew:

brew install bat

First, open your terminal and navigate to your repository. The quickest way to use bat with git diff is to pipe one into the other:

git diff | bat -l diff

The -l diff flag tells bat to highlight the input as a diff, because it can't guess the language from a pipe.

To see your actual code with the changes marked in the margin, bat has the --diff flag. Add this helper function to your ~/.zshrc file:

batdiff() {
git diff --name-only --relative --diff-filter=d -z | xargs -0 bat --diff
}

Then run batdiff. A ~ in the margin marks a modified line and a + marks a new one, with a couple of lines of context around each change.

If you want git to use bat every time, set it as the diff pager:

git config --global pager.diff "bat --style=plain --language=diff"

The --style=plain part removes the line numbers and borders, so they don't clash with git's own hunk headers. Now a normal git diff goes through bat automatically.

undefined

If you want bat to handle all of git's paging, including git log and git show, set core.pager instead:

git config --global core.pager "bat --style=plain"