1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Git全ブランチから文字列を含むファイルを一括検索

Posted at

Git リポジトリ内の すべてのブランチ に存在するファイルの中から、Git という文字列を含んでいるものを検索したい。


方法①:全ブランチを対象に文字列検索

git fetch --all

for branch in $(git for-each-ref --format='%(refname:short)' refs/remotes/); do
  echo "Checking $branch"
  git checkout --quiet $branch
  git grep -l "Git" || echo "No matches in $branch"
done

解説:

  • git fetch --all: 全てのリモートブランチ情報を取得。
  • git for-each-ref: リモートブランチを列挙。
  • git checkout: ブランチをチェックアウト(--quiet でメッセージ非表示)。
  • git grep -l "Git": ファイル内に "Git" を含むファイル名を表示。

方法②:チェックアウトせずに全ブランチで検索(高速)

ブランチをチェックアウトせずに調べたい場合はこちら:

for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
  echo "Checking $branch"
  git grep -l "Git" $branch
done

補足:

  • これはローカルブランチだけを対象とします。
  • リモートを含めたい場合は refs/remotes/ に変更するか、リモートブランチをローカルに追跡させる必要があります。

Tips

  • 大文字・小文字を区別せずに検索したい場合は -i オプションを追加:

    git grep -il "Git" $branch
    
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?