简体   繁体   English

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

[英]git ignore certain sub directory / folder and its content

I have the following directory structure: 我有以下目录结构:

ls -l experiment/

foo.sh
gen__20190425_144843
gen__20190425_144854
bar.yml

in which gen___ are folders. 其中gen___是文件夹。

How do i write the .gitignore file to: 如何将.gitignore文件写入:

  • keep track of foo.sh and bar.yml 跟踪foo.sh和bar.yml
  • and not keep track of all the gen___ folders 而不是跟踪所有gen___文件夹

I've tried this but does not work (still tracks the gen folders) 我已经尝试过了,但是不起作用(仍然跟踪gen文件夹)

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

As comments suggests, i end up using the following: 如评论所示,我最终使用以下内容:

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

The important thing is to use the naked directory name : 重要的是使用裸目录名称

  • correct: experiment/ 正确: experiment/
  • incorrect: ./experiment/ 错误: ./experiment/

experiment/gen__* should be enough as the following example shows: 如下面的示例所示, 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM