git diffで package-lock.json やテストデータの差分が表示されないようにする方法

pathspecで指定する

gitでpathを絞る仕様として pathspec がある。 : につづけて指令を書くことができ、grep時などにファイル除外やディレクトリ指定など高度なことができる。

これを利用して、 :(exclude)<path> と書いて除外することができる。

git diff -- ':(exclude)*/package-lock.json' ':(exclude)*/test/'
 
# `!` は `(exclude)` のshorthandなのでこれでもいい
git diff -- ':!*/package-lock.json' ':!*/test/'
magic signaturemagic word効果
/top通常はカレントディレクトリ以下が検索対象になるが、これをつけるとルートディレクトリから検索になる
literal*, ? といった特殊な記号をただの文字列として扱う
icaseignorecase. 大文字と小文字を区別しない
globglobパターンが使える。** で階層を任意にできる
attrattributeを利用する gitattributes
!,^exclude除外

gitattribute

https://git-scm.com/docs/gitattributes

特定のファイルパターンをbinaryとすることで除外できる。

binaryは -diff -merge -text のbuilt-in macro .gitattributes

package-lock.json binary
yarn.lock binary