¿Qué opción se debe usar para restringir el git diff a un conjunto determinado de extensiones de archivo?


¿Hay una opción para restringir git diff a un conjunto dado de extensiones de archivo?

Author: Andrew Marshall, 2011-12-19

8 answers

Sí, si te aseguras de que git expande un glob en lugar de tu shell, entonces coincidirá en cualquier nivel, por lo que algo como esto (las comillas son importantes) debería funcionar bien.

git diff -- '*.c' '*.h'
 162
Author: CB Bailey,
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
2011-12-18 21:33:46

Usa el globstar de tu shell (que hace una búsqueda recursiva)1,2:

shopt -s globstar 
git diff -- *.py **/*.py

O use buscar:

find -name '*.py' -print0 | xargs -0 git diff --

Ambas son pruebas de nombres especiales y espacios en blanco. Aunque es posible que desee filtrar los directorios que tienen la extensión. py:)


1 Me gusta hacer git diff -- {.,**}/*.py normalmente

2 Cuando globstar está habilitado, git diff -- **/*.py ya incluye ./*.py. En la página de manual de Bash: 'Si es seguido por un/, dos * s adyacentes coincidirán solo con directorios y subdirectorios.'

 8
Author: sehe,
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
2013-10-09 15:17:34

Para incluir archivos recursivamente (incluyendo el directorio actual) esto funcionó para mí:

git diff -- '***.py'
 2
Author: Bohumir Zamecnik,
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
2018-08-10 11:36:38

Para patrones de archivos simples, esto parece funcionar:

$ git ls-files -zm '*.txt' | xargs --null git diff

Espacio en blanco seguro, y también puede tener varias extensiones:

$ git ls-files -zm '*.h|*.c|*.cpp' | xargs --null git diff
 1
Author: Mat,
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
2011-12-18 21:27:01

Argumento de la línea de comandos para la extensión.

git diff *.py

Como alternativa, puedes canalizar find a git diff:

find . -name '*.py' -type f | git diff --
 0
Author: hughdbrown,
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
2011-12-18 23:29:54

Ninguna de las respuestas anteriores parece funcionar para mí bajo git bash en Windows. No estoy seguro de si es una versión (estoy usando 1.8.4) o Windows/bash; también, en mi caso, quería diferenciar dos ramas donde cada rama tenía archivos adicionales no presentes en la otra rama (por lo tanto, los basados en 'find' son negligentes).

De todos modos esto funcionó para mí (en mi ejemplo, buscando una diferencia entre archivos python):

git diff branch1 branch2 -- `git diff --summary branch1 branch2 | egrep '\.py$' | cut -d ' ' -f 5`
 0
Author: Yonatan,
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
2014-11-08 17:25:48

git diff solo mostrará las diferencias en los archivos no editados.

Encontré esta pregunta porque quería excluir archivos .info de git diff. Logré esto mediante la puesta en escena con git add *.info, lo que reduce los archivos que quedan.

 0
Author: Alan Whitelaw,
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
2015-09-14 10:46:24

Terminé con esto:

commit=<the_commit_hash_goes_here> && git diff --name-only $commit | grep -i Test | egrep -v '\.sql$' | xargs git diff $commit --

Esto muestra diferencias para la confirmación especificada solo si el nombre del archivo contiene la palabra 'test' (sin distinción de mayúsculas y minúsculas) y no termina con .sql, modifique la canalización según sea necesario para su caso.

 0
Author: MatrixManAtYrService,
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
2018-04-06 21:04:14