Determine if a file is binary using GIT

less than 1 minute read

Sometimes it’s nice to know if a file is a binary file or a text file from the command line (Such as when building an api to display files stored in a git repository).

The command is as follows:

git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 --numstat GIT_REF -- FILEPATH

Revision 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is a special git sha that points to a empty commit.

A text file will output:

git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 --numstat HEAD -- package.json
34  0  package.json

A binary file will output

git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 --numstat HEAD -- logo.png
-   -  logo.png

The output returns a tab separated list which can be piped to | cut -f1. The first column is the number of lines in the file at revision GIT_REF. If it is a binary file it will return -.

This check can also be performed without git, but will only work on files that are currently on the filesystem using the file command in bash.

A text file will output:

file --mime package.json
package.json: text/plain; charset=us-ascii

A binary file will output

file --mime logo.png
logo.png: image/png; charset=binary

Leave a Comment