ostrov040
ostrov040

Reputation: 71

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

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.

Upvotes: 2

Views: 139

Answers (2)

ostrov040
ostrov040

Reputation: 71

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                                                                                        

Upvotes: 1

VonC
VonC

Reputation: 1323175

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

Upvotes: 1

Related Questions