git除了子目录和它被忽略的目录的文件

时间:2015-06-02 05:05:01

标签: git

我使用git来管理我的源代码,我在下面的路径中有一些文件:

Debug/a.dll
Debug/b.exe
Debug/c.png
Debug/Pic/a.png
Debug/Pic/b.bmp
Debug/Pic/c.dll

我想忽略这些文件:

Debug/a.dll
Debug/b.exe
Debug/c.png

但要忽略那些文件:

Debug/Pic/a.png
Debug/Pic/b.bmp
Debug/Pic/a.dll

3 个答案:

答案 0 :(得分:2)

Git使用ans忽略或跟踪忽略路径中的文件。

在您的情况下,您需要将其添加到项目根目录中的.gitignore文件中。创建文件是否不存在

.gitignore

此示例#List of files to ignore Debug/* #List of files to exclude from the ignored pattern above !Debug/Pic !Debug/Pic/*

的内容是什么

.gitignore - 这将忽略Debug文件夹下的所有文件
Debug/* - 在这种情况下, !Debug/Pic/* 是一个特殊字符,告诉git从忽略的路径中排除给定的模式。

换句话说:
我们告诉" git忽略!文件夹下的所有文件,但要包含Debug文件夹下的所有文件。

答案 1 :(得分:1)

您可以先添加子目录,然后忽略包含目录:

git add Debug/Pic
git ignore Debug

这会产生副作用,即不向Debug / Pic添加新文件,但您可以使用&g; add -f'手动添加它们。绕过.gitignore警告。

答案 2 :(得分:1)

像往常一样,在gitignore中排除,要记住的规则是:

  

<强> It is not possible to re-include a file if a parent directory of that file is excluded (*)
  (*:除非在git 2中满足某些条件。?,见下文)

由于Debug/*会忽略文件夹Debug/Pic/,因此尝试排除该文件夹的内容将无效(!Debug/Pic/*)git 2.6或更低。

您需要先排除该文件夹。然后是它的内容。

Debug/*
!Debug/Pic/
!Debug/Pic/*

请注意,使用git 2.9.x / 2.10(2016年中期?),如果排除该文件的父目录if there is no wildcard in the path re-included,则可能会重新包含文件。

Nguyễn Thái Ngọc Duy (pclouds)正在尝试添加此功能:

所以在git 2.9+中,为了重新包含文件夹Debug/Pic/,您可以这样做:

/Debug
!Debug/Pic