user3612853
user3612853

Reputation:

Exclude specific subdirectory with gitignore

Let say there is directory structure like this:

├── dir1
│   ├── subdir_1        # do not want this
│   │   ├── file1.txt   # and this
│   │   └── file2.txt   # and this
│   ├── subdir_2        # keep me in gitignore
│   └── subdir_3        # keep me as well
└── dir2
    └── subdir4
        ├── file3.txt
        └── file4.txt

And I want to gitignore everything what is in the dir1 directory but not want to include subdir_1. Which means I want to track changes in subdir_1 and not in subdir_2 or subdir_3

How to do this ?

I already tried

/dir1/
!/dir/subdir_1

In fact my .gitignore file is much more longer and I was googling for solutions for that problem. They told me to first ignore everything with * and then negate it. But I'd prefer a more simple solution and not to refactor the existing .gitignore.

Upvotes: 2

Views: 155

Answers (1)

iamruss
iamruss

Reputation: 812

To have files in subdir_1 not ignored, try this:

dir1/*
!dir1/subdir_1*
!dir1/subdir_1/*

EDIT: tested on your specific structure :)

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   .gitignore
        new file:   dir1/subdir_1/good_file1.txt
        new file:   dir1/subdir_1/good_file2.txt
        new file:   dir2/subdir4/file3.txt
        new file:   dir2/subdir4/file4.txt

Upvotes: 1

Related Questions