Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find commits that modify file names matching a pattern in a GIT repository

I'd like to find commits in my code base that add video files to throw them out. Is there a way to look for these files in git ?

For example let's say all videos have a filename ending with the extension .wmv ; I'd like to find all commits introducing these files and get rid of them with a fixup or something.

Any ideas ?

like image 343
Bastes Avatar asked Jun 27 '11 08:06

Bastes


People also ask

How do you see what files were changed in a commit git?

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

How do I search for a filename in git?

git ls-files will give you a listing of all files in current state of the repository (the cache or index). You can pass a pattern in to get files matching that pattern. ls-files can also take a pattern. Remember to use '**/HelloWorld.pm' instead of '*/HelloWorld.pm' to search any depth of the repository for matches.


2 Answers

You can use git log with a pathspec:

git log --all -- '*.wmv' 

This will get you all commits which make changes to .wmv files. yes, this will descend into subdirectories too (but you have to surround your pathspec with single quotes, so it will be passed as is to git).

If you are only interested in commit hashes (scripting etc.) use the git rev-list machinery directly:

git rev-list --all -- '*.wmv' 

Under Windows, it might be required to use double quotes instead of single quotes around the pathspec, e.g. "*.wmv"

like image 179
knittl Avatar answered Sep 22 '22 10:09

knittl


If you want to remove these files from all your commits, consider rewriting the entire history with the filter-branch command. E.g.,

git filter-branch --index-filter 'git rm --cached --ignore-unmatch -r *.wml' HEAD 
like image 24
adl Avatar answered Sep 21 '22 10:09

adl