-1

I made some changes in a git branch of a very busy repository. Got distracted with other features and bug fixes, switched branches a few dozen times, and never pushed that change to the master.

And now I am trying to identify one of literally hundreds of branches where I made the change. The change didn't produce any new files and the only way to find it (apart from improving my memory) is to search for certain words in the source files.

Is it possible to grep for a keyword in source files across multiple git branches?

7
  • git-scm.com/docs/git-grep ? Commented Dec 13, 2024 at 20:31
  • Hmm. On second thought this might be different from the best answer on the linked question. Do you specifically want to search across only commits on all branches? Not also any descendant commits of those branches? In other words the difference between searching in the commits for branches a, b, and c and the searching through all reachable commits (all commits on all branches). Commented Dec 14, 2024 at 9:23
  • But see also this question Commented Dec 14, 2024 at 9:25
  • (correcting previous (deleted) comment) (for the above (only branches)) This is the best I found: git grep <search> $(git for-each-ref --format='%(refname:short)' 'refs/heads/**'). Unlike some git rev-list stuff it will highlight the branch name, not the commit hash. Well, the branch names look a little mangled (replacing first hyphen with colon?) but it’s seviceable. Commented Dec 14, 2024 at 12:49
  • 1
    @Michael Then you can for example match on branches with the prefix feature/ with git grep <search> $(git for-each-ref --format='%(refname:short)' 'refs/heads/feature/**'). That’s a glob (not a regex) but it’s powerful enough for patterns like that. Commented Dec 16, 2024 at 17:00

2 Answers 2

1

If you know what you are looking for, say, a text that was added or removed, you can do

git log --all -Ssome-text-added-or-removed 
Sign up to request clarification or add additional context in comments.

Comments

0

git grep is the tool for that.

You can search in specific branches or revisions:

git grep '<search term>' branch1 branch2 branch3

or search through all revisions:

git grep '<search term>' $(git rev-list --all)

3 Comments

git grep '<search term>' $(git rev-list --all) is likely to fail with argument list too long. (But git rev-list --branches should be fine.) It also won’t show/decorate the branches next to the commit hits.
Perhaps going through the reglog would be more to the point. So, HEAD@{1}, HEAD@{2}, etc
↑ You can insert git rev-list --walk-reflogs HEAD for that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.