75
49

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git diff --diff-filterが地味に優秀(削除ファイルのみ/変更ファイルのみにフィルター)

Last updated at Posted at 2018-10-04

先日、関わっているプロジェクトで不要なファイルを整理していたら、3000行も削除してしまっていました。

既存のコードをリファクタリングしたり、要らないなと思ったファイルは容赦なく削除したりしてると、masterとの差分がとんでもないことになります。「どのファイルを削除したんだっけ?」って思って git diff --name-only とかしても変更分と削除分が混ざってめちゃ見にくいです。

そんな時に、 --diff-filter=* オプション!

--diff-filter=[(A|C|D|M|R|T|U|X|B)...[*]]
    Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, ...) changed (T), are Unmerged (U), are Unknown (X), or
    have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added to the combination, all paths are selected if there is any file
    that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.

    Also, these upper-case letters can be downcased to exclude. E.g.  --diff-filter=ad excludes added and deleted paths.

    Note that not all diffs can feature all types. For instance, diffs from the index to the working tree can never have Added entries (because the set of paths included in the diff is limited by what is in
    the index). Similarly, copied and renamed entries cannot appear if detection for those types is disabled.

変更した差分だけみたい時

$ git diff upstream/master --diff-filter=M

削除した差分だけみたい時

$ git diff upstream/master --diff-filter=D

リネームした差分だけみたい時

$ git diff upstream/master --diff-filter=R

なお、逆に特定の差分を除外したいなんて時もあると思います。そんな時は、

Also, these upper-case letters can be downcased to exclude. E.g. --diff-filter=ad excludes added and deleted paths.

とあるように、小文字を指定してあげればOKです。
例えば、削除した差分だけ除外したい時は、

$ git diff upstream/master --diff-filter=d

のように書けばOKです。まあ便利。

ぜひ3000行くらい削除して「もう何をどんくらい削除したか分からん!」ってときに使ってみてください。

75
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
75
49

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?