'How to write .gitignore so that it only includes YAML files and some specific files?

I have a directory with the following contents:

.
├── .gitattributes
├── .gitignore
├── .gitignore.bak
├── coding_init.bat
├── config
│   ├── config
│   │   └── default.yml
│   └── match
│       ├── base.yml
│       └── packages
├── init.bat
├── mykey
├── packages
│   └── a.txt
├── r.bat
├── reset.bat
├── run_github.bat
└── runtime
    ├── disabledv2.ico
    ├── espanso-daemon.lock
    ├── espanso-worker.lock
    ├── espanso.lock
    ├── espanso.log
    ├── formv2.ico
    ├── icon_no_backgroundv2.png
    ├── iconv2.png
    ├── kvs
    │   ├── has_completed_wizard
    │   ├── has_displayed_welcome
    │   └── has_selected_auto_start_option
    ├── normalv2.ico
    ├── search.png
    ├── tray_explain_image.png
    └── wizardv2.ico

I want to create a .gitignore file so that only the following files are included in my commits:

  • .yml and .yaml files
  • .gitattributes
  • .gitignore

So only these files should go into my commits:

.gitattributes
.gitignore
config/config/default.yml
config/match/base.yml

I've read the git's documentation about the .gitignore but I still can't make it work. I'm using git version 2.34.0.windows.1. The following is what I've tried.

C:\test>type .gitignore
*
!*/
!*.yaml
!*.yml
!.gitignore
!.gitattributes
C:\test>git add * && git commit -m "Auto commit"
[master (root-commit) 3bfab66] Auto commit
 17 files changed, 10 insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 .gitignore
 create mode 100644 config/config/default.yml
 create mode 100644 config/match/base.yml
 create mode 100644 packages/a.txt
 create mode 100644 runtime/disabledv2.ico
 create mode 100644 runtime/espanso-daemon.lock
 create mode 100644 runtime/espanso-worker.lock
 create mode 100644 runtime/espanso.lock
 create mode 100644 runtime/espanso.log
 create mode 100644 runtime/formv2.ico
 create mode 100644 runtime/icon_no_backgroundv2.png
 create mode 100644 runtime/iconv2.png
 create mode 100644 runtime/normalv2.ico
 create mode 100644 runtime/search.png
 create mode 100644 runtime/tray_explain_image.png
 create mode 100644 runtime/wizardv2.ico

C:\test>

Update

I've created a recording to show my problem. enter image description here

Update 2

C:\kj>type .gitignore
# Ignore everything by default
*

# Don't ignore directories (so we can look inside them, for other files)
!*/

# Don't ignore these
!*.yaml
!*.yml
!.gitattributes
!.gitignore
C:\kj>
C:\kj>rm -rf .git
C:\kj>git init
Initialized empty Git repository in C:/kj/.git/

C:\kj>git add . --dry-run
add '.gitattributes'
add '.gitignore'
add 'config/config/default.yml'
add 'config/match/base.yml'
add 'runtime/disabledv2.ico'
add 'runtime/espanso-daemon.lock'
add 'runtime/espanso-worker.lock'
add 'runtime/espanso.lock'
add 'runtime/espanso.log'
add 'runtime/formv2.ico'
add 'runtime/icon_no_backgroundv2.png'
add 'runtime/iconv2.png'
add 'runtime/normalv2.ico'
add 'runtime/search.png'
add 'runtime/tray_explain_image.png'
add 'runtime/wizardv2.ico'

C:\kj>
C:\kj>rm -rf .git
C:\kj>git init
Initialized empty Git repository in C:/kj/.git/

C:\kj>git add \*.yml .gitattributes .gitignore --dry-run
fatal: \*.yml: '\*.yml' is outside repository at 'C:/kj'

C:\kj>git add *.yml .gitattributes .gitignore --dry-run
add '.gitattributes'
add '.gitignore'
add 'config/config/default.yml'
add 'config/match/base.yml'

C:\kj>


Solution 1:[1]

You have it backwards. Your .gitignore says what to ignore. When you have !*.yaml in there, you are saying to NOT ignore *.yaml files.

Get rid of the ! and you should be fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy Lester