LoginSignup
47
41

More than 5 years have passed since last update.

全ブランチから文字列検索・全コミットから文字列検索

Last updated at Posted at 2017-06-30

Stack Overflowで紹介されていた技法なのですが、日本語で紹介した記事がどこにもないようだったので。
https://stackoverflow.com/questions/18278774/search-git-remote-all-branches-for-file-contents

全ブランチから文字列検索する

git grep 【キーワード】 $(git branch -a --format='%(objectname) %(refname:short)' | sort | uniq -w 40 | cut -c 42-)

全コミットから文字列検索する

git grep 【キーワード】 $(git rev-list --all)

仕組み

全ブランチを(コミットの重複なく)列挙するには以下のように書きます。

git branch -a --format='%(objectname) %(refname:short)' | sort | uniq -w 40 | cut -c 42-
  1. ローカル+リモートブランチをハッシュ値 ブランチ名という書式で出力し
  2. 先頭から40文字(ハッシュ値)で重複削除し
  3. 42文字目以降(ブランチ名)だけ切り出す

べしと書きました。

全コミットを列挙するコマンドは最初からあります。

git rev-list --all

git grepというコマンドは指定コミット(複数指定可)から指定文字列を検索してくれるものです。そこで、存在するブランチ全部または存在するコミット全部をリスト化してgit grepに渡してやることで目的が達成できるという具合。

リモートにある分を取りこぼさないよう、git fetchしてからどうぞ。

47
41
2

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
47
41