有没有办法将目录添加到Git但排除.gitgnore中的扩展名?

vfc*_*sts 7 git

有没有办法将目录添加到Git,但在.gitgnore中排除扩展名?

例如,当我执行命令添加诸如的目录时

git add console/*
Run Code Online (Sandbox Code Playgroud)

git响应回应

$ git add console/*
The following paths are ignored by one of your .gitignore files:
console/networkprobing.ppu
console/opsdatamodule.lrs
console/opsdatamodule.o
console/opsdatamodule.ppu
console/rcacsSystemDM.compiled
console/rcacsSystemDM.o
console/rcacsSystemTestProcs.o
console/rcacsSystemTestProcs.ppu
Use -f if you really want to add them.
fatal: no files added
Run Code Online (Sandbox Code Playgroud)

如何使命令自动排除.gitignore扩展?还有其他选择吗?

Arr*_*ter 10

git add console/*
Run Code Online (Sandbox Code Playgroud)

问题是通配符是通过shell globing扩展的,而不是直接传递给git,所以git-add实际上是传递给控制台目录[1]中的每个文件.因为git-add正在接收目录中的每个文件作为参数,所以它认为你可能真的试图添加一个被忽略的文件,这就是为什么会有一个警告,并提示使用-f,如果这是你真正想要做的事情.

解决方案是不使用shell globing而是只是像这样传递目录.

git add console
Run Code Online (Sandbox Code Playgroud)

类似地,将所有内容添加到repo的正确方法(包括以'.'开头的文件)除了忽略文件[2]到repo之外是使用git add .而不是git add *.

[1]除了以"."开头的.假设您处于类似shell的状态,而不是从Windows cmd.exe命令提示符处尝试此操作.如果您使用的是Windows命令提示符,则应切换到使用msysgit中的Git-Bash shell或来自cygwin的shell,具体取决于您安装git的方式.
[2] .gitignore仅用于防止未跟踪的文件被跟踪,如果该文件已被git跟踪,则它将继续被正常处理,直到从repo中删除.这可以在不影响实际文件的情况下完成git rm --cached filename.


mip*_*adi 7

.gitignore可以采用shell样式的glob模式.因此,例如,如果要忽略.jpg可以添加的所有文件

# Ignore all JPEGs
*.jpg
Run Code Online (Sandbox Code Playgroud)

.gitignore.如果您只想忽略新目录中的JPEG,则可以这样做

new_directory/*.jpg
Run Code Online (Sandbox Code Playgroud)

然后正常添加目录.