1

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>
danhekun
  • 25
  • 1
  • 4
  • Does this answer your question? [Make .gitignore ignore everything except a few files](https://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files) – SwissCodeMen Jan 31 '22 at 22:08
  • @SwissCodeMen I've read that question. If you read carefully you will find that the .gitignore file in my question is the same as the accepted answer of that question. – danhekun Jan 31 '22 at 22:53
  • Hi @danhekun, git is adding files that should be ignored because you're using a glob pattern. Instead of doing `git add *`, do `git add --all`. I updated my answer with an explanation of why this is the case. – Alecto Irene Perez Feb 01 '22 at 08:24

2 Answers2

1

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

Alecto Irene Perez
  • 10,321
  • 23
  • 46
  • Hi @Alecto, thanks for your answer! I've tried your suggestion but it doesn't work. I uploaded a small recording in the question. Could you take a look where is the problem? – danhekun Feb 01 '22 at 09:13
  • Hi @danhekun - my best guess is that git add --all works differently on windows. Could you try `git add . --dry-run`? This will recursively add files starting with the current directory, but ignoring everything in the gitignore. If that doesn't work, could you try `git add \*.yml .gitattributes .gitignore --dry-run`? – Alecto Irene Perez Feb 01 '22 at 19:32
  • Hi @Alecto, it seems `git add *.yml .gitattributes .gitignore --dry-run` works. See my question update. I really appreciate your help. – danhekun Feb 01 '22 at 20:08
  • 1
    It's not because gratitude is hard for me, it's because I don't have enough reputation to vote – danhekun Feb 01 '22 at 20:12
  • I'm happy we were able to find a solution to the problem! I wish I could have been more helpful, but I don't have a development environment set up on windows for me to test stuff. – Alecto Irene Perez Feb 01 '22 at 21:26
0

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.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • Sorry for the misinterpretation. I updated my question. Actually, I need the *.yaml files in my commit. – danhekun Jan 31 '22 at 21:52