Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.4k points)

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.

1 Answer

0 votes
by (27.5k points)

In order to search Git branches for a file or directory git log: 

% git log --all -- somefile

commit 55d2069a092e07c56a6b4d321509ba7620664c63

Author: some author <[email protected]>

Date:   Tue Dec 19 14:16:22 2008 -0800

    added somefile

% git branch -a --contains 55d2069


Supports globbing, too:

% git log --all -- '**/myfile.png'

The single quotes are necessary (at least if using the bash shell) so the shell passes the glob pattern to git unchanged, instead of expanding it (just like with Unix find).

Browse Categories