使用 .gitignore 忽略子目录中的特定文件
Unignore specific Files in Subdirectory with .gitignore
我在让 .gitignore 执行我想要的操作时遇到问题。我的文件夹结构如下所示:
assets
├── img
| ├── thousands
| ├── of
| ├── folders
| ├── KEEP_SOMETHING_IN_THIS_FOLDER
| | ├── another
| | ├── thousands
| | ├── of
| | ├── folders
| | ├── KEEP_THIS_FILE_1.jpg
| | ├── KEEP_THIS_FILE_2.jpg
| | ├── KEEP_THIS_FILE_3.jpg
我尽量保留这三个jpg。我试过了
/assets/img/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/
/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*/
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE_*.jpg
您需要在指定文件夹中创建.gitignore 文件。在 "KEEP_SOMETHING_IN_THIS_FOLDER" 你的情况下。并写下这几行:
/**
/*.jpg
!not_ignore.jpg
首先提交此 .gitignore
文件,然后尝试提交您的文件。
但是,如果您的文件已经被暂存(跟踪),请尝试 git rm --cached <filePath>
这些文件。
你很接近:
/assets/img/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/
# changed this:
# /assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*/
# to:
# /assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*
/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE_*.jpg
对于子文件夹(第 3 行),您不需要在忽略尾部斜杠。
当从 *nix 工作站 运行 man gitignore
并查看 PATTERN FORMAT
部分时,我发现以下语句:
- An optional prefix "!" which negates the pattern; any matching file
excluded by a previous pattern will become included again. It
is not possible to re-include a file if a parent directory of that
file is excluded. Git doesn't list excluded directories for
performance reasons, so any patterns on contained files have no
effect, no matter where they are defined. Put a backslash ("\") in
front of the first "!" for patterns that begin with a literal "!", for
example, "!important!.txt".
注意粗体部分。在您介绍的情况下,如果您想忽略属于您忽略的目录的子目录下的文件,则 "un-ignore" !
运算符将不起作用。相反,您必须更具体地使用忽略模式。
试穿尺码:
/assets/img/*/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE*.jpg
你可以这样做:
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
喜欢:
# exclude /filepath/includes/a.java
/*
!/filepath
/filepath/*
!/filepath/includes
/filepath/includes/*
!/filepath/includes/a.java
顺便说一下,如果您想忽略所有 jar (*.jar),但又想排除
/src/main/webapp/WEB-INF/lib/*.jar
在你的 .gitignore
中执行此操作
# Package Files #
*.jar
# exclude /src/main/webapp/WEB-INF/lib/*.jar
!/src/main/webapp/WEB-INF/lib/*.jar
我在让 .gitignore 执行我想要的操作时遇到问题。我的文件夹结构如下所示:
assets
├── img
| ├── thousands
| ├── of
| ├── folders
| ├── KEEP_SOMETHING_IN_THIS_FOLDER
| | ├── another
| | ├── thousands
| | ├── of
| | ├── folders
| | ├── KEEP_THIS_FILE_1.jpg
| | ├── KEEP_THIS_FILE_2.jpg
| | ├── KEEP_THIS_FILE_3.jpg
我尽量保留这三个jpg。我试过了
/assets/img/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/
/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*/
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE_*.jpg
您需要在指定文件夹中创建.gitignore 文件。在 "KEEP_SOMETHING_IN_THIS_FOLDER" 你的情况下。并写下这几行:
/**
/*.jpg
!not_ignore.jpg
首先提交此 .gitignore
文件,然后尝试提交您的文件。
但是,如果您的文件已经被暂存(跟踪),请尝试 git rm --cached <filePath>
这些文件。
你很接近:
/assets/img/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/
# changed this:
# /assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*/
# to:
# /assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*
/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE_*.jpg
对于子文件夹(第 3 行),您不需要在忽略尾部斜杠。
当从 *nix 工作站 运行 man gitignore
并查看 PATTERN FORMAT
部分时,我发现以下语句:
- An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".
注意粗体部分。在您介绍的情况下,如果您想忽略属于您忽略的目录的子目录下的文件,则 "un-ignore" !
运算符将不起作用。相反,您必须更具体地使用忽略模式。
试穿尺码:
/assets/img/*/*
!/assets/img/KEEP_SOMETHING_IN_THIS_FOLDER/KEEP_THIS_FILE*.jpg
你可以这样做:
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
喜欢:
# exclude /filepath/includes/a.java
/*
!/filepath
/filepath/*
!/filepath/includes
/filepath/includes/*
!/filepath/includes/a.java
顺便说一下,如果您想忽略所有 jar (*.jar),但又想排除
/src/main/webapp/WEB-INF/lib/*.jar
在你的 .gitignore
中执行此操作# Package Files #
*.jar
# exclude /src/main/webapp/WEB-INF/lib/*.jar
!/src/main/webapp/WEB-INF/lib/*.jar