如何使用.gitignore仅在git中包含特定文件夹?

fen*_*nec 3 git gitignore git-branch

所以我想忽略除文件和特定文件夹之外的所有内容:

rben@ubuntu:~/sites$ ls
development-box  puppet  sites  Vagrantfile  www-cms  www-common  www-mmfa
rben@ubuntu:~/sites$ cat .gitignore
*
!Vagrantfile
!puppet/
rben@ubuntu:~/sites$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       Vagrantfile
nothing added to commit but untracked files present (use "git add" to track)
Run Code Online (Sandbox Code Playgroud)

我期待在未跟踪文件列表中看到puppet.我怎么能只包含目录木偶?

nic*_*715 10

好的,试试下面的gitignore文件:

# Ignore everything in repository root 
/*

# Files to not ignore
!/.gitignore
!/some_other_files

# Folder to not ignore
!/puppet/
Run Code Online (Sandbox Code Playgroud)

关键点是之前的斜线*.

在您的情况下*受影响的文件也在puppet.

因此,为了安全起见,请使用绝对路径,例如我的示例

我刚才问了一个类似的问题:Git忽略除子文件夹之外的目录中的所有内容