tags:

views:

2319

answers:

3

In Git, how could I search for a file or directory by path across a number of branches?

I've written something in a branch, but I don't remember which one. Now I need to find it.

Clarification: I'm looking for a file which I created on one of my branches. I'd like to find it by path, and not by its contents, as I don't remember what the contents are.

+2  A: 

git ls-tree might help. To search across all existing branches:

for branch in `git branch | sed 's/\*//'`; do
  echo $branch :; git ls-tree $branch | grep '<foo>'
done
ididak
A few hopefully helpful comments: (a) You probably want to add "-r" to "git ls-tree" so that it'll find the file even if it's in a subdirectory. (b) A perhaps neater alternative to the first line would be to use "git for-each ref", e.g. "for branch in `git for-each-ref --format="%(refname)" refs/heads`; do" (c) "git ls-tree --name-only" will make the output tidier (d) it might be worth pointing out in your answer that there's an advantage of this over Dustin's solution, namely that you don't need to exactly know the filename - you can search by regular expression matching any part of the path
Mark Longair
A: 

You could use gitk --all and search for commits "touching paths" and the pathname you are interested in.

Greg Hewgill
+17  A: 

git log will find it for you:

% git log --all -- somefile

commit 55d2069a092e07c56a6b4d321509ba7620664c63
Author: Dustin Sallings <[email protected]>
Date:   Tue Dec 16 14:16:22 2008 -0800

    added some file
% git branch --contains 55d2069
  otherbranch
Dustin