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

时间:2021-12-03 21:05:05

So i would like ignore everything except a file and a specific folder:

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

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)

I am expecting to see puppet in the list of untracked files. How can i include only the directory puppet?

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

1 个解决方案

#1


8  

ok nice, try the following gitignore file:

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

# Ignore everything in repository root 
/*

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

# Folder to not ignore
!/puppet/

The key point is the slash before the *.

关键点是*之前的斜线。

In your case the * affected also to the files in puppet.

在你的情况下*也影响了木偶中的文件。

So to be on the safe side use absolute paths like in my example

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

I asked a similar question a while ago: Git Ignore everything in a directory except subfolders

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

#1


8  

ok nice, try the following gitignore file:

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

# Ignore everything in repository root 
/*

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

# Folder to not ignore
!/puppet/

The key point is the slash before the *.

关键点是*之前的斜线。

In your case the * affected also to the files in puppet.

在你的情况下*也影响了木偶中的文件。

So to be on the safe side use absolute paths like in my example

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

I asked a similar question a while ago: Git Ignore everything in a directory except subfolders

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