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

GitFindFilenames

Git Problem Overview


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 ?

Git Solutions


Solution 1 - Git

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"

Solution 2 - Git

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

Solution 3 - Git

You can try this:

git log --follow *.wmv

this will list all commits (with hash) that modified wmv files.

Solution 4 - Git

Yup, like mentioned, I think the thinko is that removing the commits that introduce them is not going to remove the blobs

See http://progit.org/book/ch9-7.html#removing_objects for an extensive treatment of the subject and examples

Solution 5 - Git

If the goal is to remove the files from the repository (thus rewriting history), use the BFG Repo-Cleaner, e.g.:

bfg --delete-files '*.wmv' --private --no-blob-protection

If the files are relevant, you can keep them under version control using Git LFS. To migrate (also rewriting history), you do something such as:

git-lfs-migrate \
    -s original.git  \
    -d converted.git \
    -l https://user:[email protected]:8080 \
    '*.wmv'

To simply list or examine the commits, I refer to knittl's answer:

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

Solution 6 - Git

This can work in gitk as well, using the View / New View / Enter files and directories to include, one per line box.

But note that you need a wildcard that covers the path section of the filename, or else nothing will show.

eg if you have had a file called backup-script.sh, with a varied life (!) appearing in different places in the file tree and you want to see all versions, then you must specify:

*/backup-script.sh

Solution 7 - Git

To just view the commit hashes and the relevant file names for each commit you can use:

git rev-list --all -- '*.wmv' $1 | while read x; do git diff-tree --name-only -r $x; done | grep -E '((\.wmv$)|(^[^\.]+$))'

This will print out the commit hash followed by any filenames that matching the search string.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBastesView Question on Stackoverflow
Solution 1 - GitknittlView Answer on Stackoverflow
Solution 2 - GitadlView Answer on Stackoverflow
Solution 3 - GitEriko MoraisView Answer on Stackoverflow
Solution 4 - GitseheView Answer on Stackoverflow
Solution 5 - GitfiliposView Answer on Stackoverflow
Solution 6 - GitMikeWView Answer on Stackoverflow
Solution 7 - GitwyattisView Answer on Stackoverflow