查找修改与 GIT 存储库中模式匹配的文件名的提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6490454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find commits that modify file names matching a pattern in a GIT repository
提问by Bastes
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 ?
我想在我的代码库中找到添加视频文件以将它们丢弃的提交。有没有办法在 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.
例如,假设所有视频的文件名都以扩展名 .wmv 结尾;我想找到所有引入这些文件的提交,并通过修复或其他方式摆脱它们。
Any ideas ?
有任何想法吗 ?
回答by knittl
you can use git log
with a pathspec:
您可以使用git log
路径规范:
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).
这将为您提供对 .wmv 文件进行更改的所有提交。是的,这也将下降到子目录中(但您必须用单引号将路径规范括起来,因此它将按原样传递给 git)。
if you are only interested in commit hashes (scripting etc.) use the git rev-list
machinery directly:
如果您只对提交哈希(脚本等)感兴趣,请git rev-list
直接使用该机器:
git rev-list --all -- '*.wmv'
回答by adl
If you want to remove these files from all your commits, consider rewriting the entire history with the filter-branch
command. E.g.,
如果要从所有提交中删除这些文件,请考虑使用该filter-branch
命令重写整个历史记录。例如,
git filter-branch --index-filter 'git rm --cached --ignore-unmatch -r *.wml' HEAD
回答by Eriko Morais
You can try this:
你可以试试这个:
git log --follow *.wmv
this will list all commits (with hash) that modified wmv files.
这将列出修改 wmv 文件的所有提交(带有哈希)。
回答by wyattis
To just view the commit hashes and the relevant file names for each commit you can use:
要查看每个提交的提交哈希和相关文件名,您可以使用:
git rev-list --all -- '*.wmv' | 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.
这将打印出提交哈希,后跟与搜索字符串匹配的任何文件名。
回答by sehe
Yup, like mentioned, I think the thinko is that removing the commits that introduce them is not going to remove the blobs
是的,就像提到的那样,我认为删除引入它们的提交不会删除 blob
See http://progit.org/book/ch9-7.html#removing_objectsfor an extensive treatment of the subject and examples
有关主题和示例的广泛处理,请参阅http://progit.org/book/ch9-7.html#removing_objects
回答by filipos
If the goal is to remove the files from the repository (thus rewriting history), use the BFG Repo-Cleaner, e.g.:
如果目标是从存储库中删除文件(从而重写历史记录),请使用BFG Repo-Cleaner,例如:
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将它们置于版本控制之下。要迁移(也重写历史记录),您可以执行以下操作:
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:
为了简单地列出或检查提交,我参考了 knittl 的回答:
git rev-list --all -- '*.wmv'
git log --all -- '*.wmv'
回答by MikeW
This can work in gitk as well, using the View / New View / Enter files and directories to include, one per line box.
这也可以在 gitk 中使用,使用 View / New View / Enter 文件和目录来包含,每行一个框。
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 的文件,在文件树的不同位置出现不同的生命 (!),并且您想查看所有版本,那么您必须指定:
*/backup-script.sh