¿Cómo puedo buscar un archivo o directorio en las ramas de Git?


En Git, ¿cómo podría buscar un archivo o directorio por ruta a través de varias ramas?

He escrito algo en una rama, pero no recuerdo cuál. Ahora necesito encontrarlo.

Aclaración : Estoy buscando un archivo que creé en una de mis ramas. Me gustaría encontrarlo por el camino, y no por su contenido, ya que no recuerdo cuáles son los contenidos.

 261
Author: Belphegor, 2008-12-16

6 answers

Git log lo encontrará por ti:

% git log --all -- somefile

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

    added somefile
% git branch -a --contains 55d2069
  otherbranch

También soporta globbing:

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

Las comillas simples son necesarias (al menos si se usa el shell bash) para que el shell pase el patrón glob a git sin cambios, en lugar de expandirlo (al igual que con Unix find).

 342
Author: Dustin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-11 23:34:35

Git ls-tree podría ayudar. Para buscar en todas las ramas existentes:

for branch in `git for-each-ref --format="%(refname)" refs/heads`; do
  echo $branch :; git ls-tree -r --name-only $branch | grep '<foo>'
done

La ventaja de esto es que también puede buscar el nombre del archivo con expresiones regulares.

 58
Author: ididak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-17 13:01:32

Aunque @ididak response es bastante genial, y @Handyman5 proporciona un script para usarlo, encontré poco restringido ese enfoque.

A veces necesitas buscar algo que pueda aparecer/desaparecer con el tiempo, así que ¿por qué no buscar en todas las confirmaciones? Además de eso, a veces necesitas una respuesta detallada, otras veces solo confirmaciones. Aquí hay dos versiones de esas opciones. Ponga estos scripts en su ruta:

Git-find-file

for branch in $(git rev-list --all)
do
  if (git ls-tree -r --name-only $branch | grep --quiet "$1") 
  then
     echo $branch
  fi
done

Git-find-file-verbose

for branch in $(git rev-list --all)
do
  git ls-tree -r --name-only $branch | grep "$1" | sed 's/^/'$branch': /'
done

Ahora puedes hacer

$ git find-file <regex>
sha1
sha2

$ git find-file-verbose <regex>
sha1: path/to/<regex>/searched
sha1: path/to/another/<regex>/in/same/sha
sha2: path/to/other/<regex>/in/other/sha

Vea que usando getopt puede modificar ese script para alternar la búsqueda de todos los commits, refs, refs/heads, been verbose, etc.

$ git find-file <regex>
$ git find-file --verbose <regex>
$ git find-file --verbose --decorated --color <regex>

Checkout https://github.com/albfan/git-find-file para una posible implementación

 16
Author: albfan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:18:20

Puedes usar gitk --all y buscar confirmaciones "touching paths" y el nombre de ruta que te interesa.

 9
Author: Greg Hewgill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-12-16 21:31:20

Copie y pegue esto para usar git find-file SEARCHPATTERN

Imprimiendo todas las ramas buscadas:

git config --global alias.find-file '!for branch in `git for-each-ref --format="%(refname)" refs/heads`; do echo "${branch}:"; git ls-tree -r --name-only $branch | nl -bn -w3 | grep "$1"; done; :'

Imprimir solo ramas con resultados:

git config --global alias.find-file '!for branch in $(git for-each-ref --format="%(refname)" refs/heads); do if git ls-tree -r --name-only $branch | grep "$1" > /dev/null; then  echo "${branch}:"; git ls-tree -r --name-only $branch | nl -bn -w3 | grep "$1"; fi; done; :'

Estos comandos agregarán algunos scripts de shell mínimos directamente a su ~/.gitconfig como alias global de git.

 5
Author: lumbric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-13 14:33:04

Una implementación bastante decente del comando find para repositorios git se puede encontrar aquí: https://github.com/mirabilos/git-find

 0
Author: Dominik George,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-13 22:17:15