git 忽略包含 <pattern to ignore> 的文件名

标签 git gitignore

我试图告诉 git 忽略文件名中某处有 _autosave 的文件。此类文件的示例是:

hats/TFCB_ATV_TOP_HAT/_autosave-TFCB_ATV_HAT.kicad_pcb

在正则表达式中,我会简单地使用模式 ^.*_autosave.*

当然,git 不使用正则表达式来评估其 .gitignore 文件。我读了一些书,觉得 *_autosave* 会起作用,但它不起作用。

告诉 git 忽略这些文件的正确语法是什么?

最佳答案

将此行添加到您的 .gitignore

*_autosave*

根据git help gitignore

patterns match relative to the location of the .gitignore file

类似 *_autosave* 的模式匹配名称中某处包含“_autosave”的文件或目录。

Two consecutive asterisks ("**") in patterns mathed against full pathname may have special meaning

A leading "**" followed by a slash means match in all directories.

但是“**/”在某些环境中是多余的。

编辑:

我工作的机器(使用 git 1.7.1)不支持 dubbel asterisk,但是使用 *_autosave* 它排除了文件。

这是一个简单的测试脚本(适用于 Linux)

DIR="$(mktemp -d)"
git init $DIR/project1
cd $DIR/project1
cat > .gitignore <<EOF
**/*_autosave*
*_autosave*
EOF
mkdir dir1
touch README foo_autosave dir1/bar_autosave
git status

rm -rf $DIR/project1

关于git 忽略包含 <pattern to ignore> 的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335459/

相关文章:

git - 当我在 git 中输入 ssh-keygen -t rsa -C "your@email.com"时,出现 "Too many arguments"错误

git - 在 python 子进程中使用 git shortlog 获取每个文件的贡献者

java - 如何在给定 commit-sha 的情况下使用 JGit 读取 Git 注释

git - 你什么时候会使用 .git/info/exclude 而不是 .gitignore 来排除文件?

git - 在 Git 中添加要跟踪的目录;错误信息;添加的大小限制?

git - Monolith git repo 与 micro repos

git - .gitignore : How do I ignore nested directories?

git - .gitignore 仅在本地

git - Git 如何忽略以数字开头的文件?

git - 如何让 Git 忘记已跟踪但现在位于 .gitignore 中的文件?