简体   繁体   English

git 如何忽略某个目录中的所有文件和子目录,但有一些例外?

[英]How to git ignore all files and subdirectories from a certain directory except with some exceptions?

I am trying to ignore all files and subdirectories from the directory .config that is located at the root of the repository except the file .config/vimb/config and the subdirectory .config/ranger/ .我试图忽略位于存储库根目录的.config目录中的所有文件和子目录,但文件.config/vimb/config和子目录.config/ranger/除外。

This is my .gitconfig :这是我的.gitconfig

# Ignore the contents of .config
.config/*
 
# Avoid Blocking
!.config/
!.config/vimb
!.config/vimb/config
!.config/ranger/

As a result git seems to ignore the entire directory event the files and subdirectories that are maked with the !结果 git 似乎忽略了整个目录事件,使用! wildcard.通配符。

For a record I also tried the following:作为记录,我还尝试了以下方法:

# Ignore Everything
*

# Add files
!somefile1
!somefile2

# Ignore Config
.config/*
 
# Avoid Blocking 
!.config/
!.config/vimb
!.config/vimb/config
!.config/ranger/

Also, I attempted ignoring everything and using !另外,我试图忽略所有内容并使用! to add each individual file and directory to be staged.添加要暂存的每个单独的文件和目录。

As of now, I used the following sources to resolve the issue:截至目前,我使用以下来源解决了这个问题:

Thanks for your help in advance.提前感谢您的帮助。

First, you can check at any point why a file is still ignored with:首先,您可以随时检查为什么文件仍然被忽略:

git check-ignore -v -- path/to/file

Second, You need to ignore files only, and exclude folders from your ignore rule, if you want then to be able to exclude files in them:其次,您只需要忽略文件,并从忽略规则中排除文件夹,如果您希望能够排除其中的文件:

.config/**
!.config/**/
!.config/vimb/config/**
!.config/ranger/**

This is because:这是因为:

It is not possible to re-include a file if a parent directory of that file is excluded如果排除了该文件的父目录,则无法重新包含该文件

This is The solution that I came up with.这是我想出的解决方案。 To sum up, my objective was to exclude all files and subdirectories from .config directory except the .config/ranger directory with all files and directories in it and the .config/vimb/config configuration file.总而言之,我的目标是从.config目录中排除所有文件和子目录,除了.config/ranger目录以及其中的所有文件和目录以及.config/vimb/config配置文件。 The following .gitignore code did the job.以下.gitignore代码完成了这项工作。

# Block 
.config/**

# Avoid Blocking 
!.config/
!.config/**/
!.config/vimb/config
!.config/ranger/
!.config/ranger/**
!.config/ranger/**/
!.config/ranger/init.rc

git status command outputs the following as expected: git status命令按预期输出以下内容:

    new file:   .config/ranger/commands.py                                                                         │
    new file:   .config/ranger/rc.conf                                                                             │
    new file:   .config/vimb/config                                                                                │
    new file:   .gitignore                                                                                        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM