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

时间:2014-04-25 19:06:32

标签: git git-branch gitignore

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

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)

我希望在未跟踪文件列表中看到puppet。我怎么才能只包含目录木偶?

1 个答案:

答案 0 :(得分:9)

好的,请尝试以下gitignore文件:

# Ignore everything in repository root 
/*

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

# Folder to not ignore
!/puppet/

关键点是*之前的斜杠。

在您的情况下,*也会影响puppet中的文件。

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

我刚才问了一个类似的问题:Git Ignore everything in a directory except subfolders