Search code examples
gitgitignore

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

  • Your gitignore would look like this:

    # 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
    

    Don't use glob patterns here

    To add untracked files automatically, use git add --all. DO NOT use git add *. This is a glob pattern. It's interpreted by your shell, not by git, and as a result it doesn't know anything about what is or isn't in the git ignore.

    What happens with a glob pattern?

    The shell expands git add * to git add coding_init.bat config init.bat mykey packages r.bat reset.bat run_github.bat runtime, and from git's perspective it looks like you're saying "add these files anyway, even though I said to ignore them"

    On my system, git actually detects that you're trying to add ignored files:

    enter image description here

    But this option may be off on your system.

    You don't need a glob pattern here

    As stated before, git add --all will do what you want, and it'll only add files that aren't ignored in your .gitignore.

    At that point, you can commit your code, as intended.

    enter image description here