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

Viewed 79

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/.

This is my .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 ! 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.

2 Answers

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. The following .gitignore code did the job.

# 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:

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