git 忽略某些子目录/文件夹及其内容

git ignore certain sub directory / folder and its content

我的目录结构如下:

ls -l experiment/

foo.sh
gen__20190425_144843
gen__20190425_144854
bar.yml

其中 gen___ 个文件夹。

如何将 .gitignore 文件写入:

我已经试过了,但没有用(仍然跟踪 gen 文件夹)

./experiment/gen__*/*
!./experiment/foo.sh
!./experiment/bar.yml

正如评论所暗示的那样,我最终使用了以下内容:

experiment/gen__*/*
!experiment/foo.sh
!experiment/bar.yml

重要的是使用裸目录名:

  • 正确:experiment/
  • 不正确:./experiment/

experiment/gen__* 应该足够了,如下例所示:

~$ mkdir test
~$ cd test
~/test$ git init
Initialized empty Git repository in /home/mh/test/.git/
~/test$ touch foo.sh
~/test$ touch bar.yml
~/test$ mkdir gen__20190425_144843
~/test$ touch gen__20190425_144843/1
~/test$ touch gen__20190425_144843/2
~/test$ mkdir gen__20190425_144854
~/test$ touch gen__20190425_144854/3
~/test$ touch gen__20190425_144854/4
~/test$ tree
.
├── bar.yml
├── foo.sh
├── gen__20190425_144843
│   ├── 1
│   └── 2
└── gen__20190425_144854
    ├── 3
    └── 4

2 directories, 6 files
~/test$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    bar.yml
    foo.sh
    gen__20190425_144843/
    gen__20190425_144854/

nothing added to commit but untracked files present (use "git add" to track)
~/test$ echo "gen__*" > .gitignore
~/test$ cat .gitignore 
gen__*
~/test$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore
    bar.yml
    foo.sh

nothing added to commit but untracked files present (use "git add" to track)