0
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 Bash

コマンド

以下のコマンドを使うと、リモートからローカルに引っ張ってきたすべてのブランチに対して特定の単語を一度に検索できます。実行前にgit pullなどで最新のブランチ情報が取得しておきます。

検索コマンド
git for-each-ref --format="%(refname:short)" refs/remotes/ | grep -v HEAD | while read branch; do echo -e "\nブランチ: $branch"; git grep -l "検索単語" $branch; done

「検索単語」の部分に、探している単語(例: hogeA や hogeFunctionB)を入れて使います。

コマンドについて このコマンドは以下の3つの処理を組み合わせています。
  1. リモートブランチの一覧取得
    git for-each-ref --format="%(refname:short)" refs/remotes/

  2. HEADを除外
    grep -v HEAD

  3. 各ブランチで検索を実行
    while read branch; do ... git grep -l "検索単語" $branch; done

便利なオプション

  • -i:大文字小文字を区別しない検索
  • -e:正規表現を使った検索
  • -- "*.js":特定の拡張子のファイルのみ検索

コマンド実行結果

検索単語をhogeFunctionとして検索してみると、以下のように hogeFunction は origin/feature/A と origin/feature/B のブランチにあることが分かります。

実行結果
ブランチ: origin/feature/A
origin/feature/A:src/utils/helper.js

ブランチ: origin/feature/B
origin/feature/B:src/fuga/fugaLogic.js
origin/feature/B:src/utils/helper.js

ブランチ: origin/feature/C
(何も表示されない = このブランチでは見つからなかった)

ブランチ: origin/main
(何も表示されない = このブランチでは見つからなかった)

単語の詳細検索

どのブランチの何のファイルに対象の単語があるか分かったら、探してる単語の前後の行を出力させて、ソースの文脈を調べてみます。

特定のファイルを指定して検索してみます。

ファイルを指定して検索
git grep -n -A 3 -B 1 "hogeFunction" origin/feature/A -- src/utils/helper.js
実行結果
origin/feature/A:src/utils/helper.js:1:function hogeFunction(paramA, paramB) {
origin/feature/A:src/utils/helper.js-2-  return paramA + paramB * 2;
origin/feature/A:src/utils/helper.js-3-}

ファイルパスを指定しなくても、ブランチ内の全ファイルから検索することも可能です。

ファイル指定なし(ブランチ内の全ファイルを検索)
git grep -n -A 3 -B 1 "hogeFunction" origin/feature/B
実行結果
origin/feature/B:src/fuga/fugaLogic.js-1-function fugaLogic() {
origin/feature/B:src/fuga/fugaLogic.js:2:  return hogeFunction(5, 10);
origin/feature/B:src/fuga/fugaLogic.js-3-}
--
origin/feature/B:src/utils/helper.js:1:function hogeFunction(paramA, paramB) {
origin/feature/B:src/utils/helper.js-2-  return paramA + paramB * 2;
origin/feature/B:src/utils/helper.js-3-}

変更履歴を表示

「hogeFunction」という単語が変更されたコミットを表示します。

どのコミットで単語が変更されたか検索
git log -p -S "検索単語" origin/feature/A
実行結果
commit d042d ~~~ 908 (origin/feature/A, feature/A)
Author: enumura1  ~~~ 
Date:   Sun Mar 16 00:37:37 2025 +0900

    Fix encoding to UTF-8

diff --git a/src/utils/helper.js b/src/utils/helper.js
index ~~~
Binary files a/src/utils/helper.js and b/src/utils/helper.js differ

エラーの調査時など、開発中に効率的なコード検索ができるようになれば幸いです。

0
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
0
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?