LoginSignup
99
75

More than 5 years have passed since last update.

git-grepの使い方

Last updated at Posted at 2018-12-28

この記事なに?

  • gitで管理されているソースだけgrepするのに使えるgit-grepコマンドを紹介する記事
  • IDEのgrepも便利だけど、Githubのプルリクに貼るのには不便
  • でも、git-grepコマンドであれば、プルリクに貼ることもできて便利!!

使い方

公式ドキュメントはこちら
https://git-scm.com/docs/git-grep

行番号を表示する場合はこちらを実行しておく

出力結果に行番号を表示する
$ git config --global grep.lineNumber true

正規表現を使いたい

-E オプション

$ git grep -E "regexp"

複数の単語が絡んだ文字を検索したい

--and オプション

$ git grep -e 'first word' --and -e 'second word'

単語でマッチングさせたい

-wオプション
単語違いを弾くことができる

$ git grep -w 'service'
input
service
services
service/hoge
service-hoge
output
service
service/hoge
service-hoge

ファイル名のみ出力させたい

-l オプション
指定した単語を含むファイル名のみ出力できる

$ git grep -l 'hoge' 

大文字・小文字両方検索したい

-i オプション
大文字・小文字両方検索できる

$ git grep -i 'hoge'

特定のディレクトリのみ検索したい

-- オプション
特定のディレクトリのみ検索できる

$ git grep 'hoge' -- 'app/controllers/'

特定のディレクトリを除外したい

-- オプションでディレクトリの指定に ':!' を付ける
特定のディレクトリだけ除外できる

$ git grep 'hoge' -- ':!app/assets/'
99
75
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
99
75