2013-10-18 111 views
5

我想忽略除www目录中的.htaccess文件以外的所有文件。让.gitignore不要忽略顶级目录中的.htaccess文件

我有这样的:

# Ignore everything in this directory 
* 
# Except this file 
!.gitignore 
!/www/.htaccess 

而且我也试过这样:

# Ignore everything in this directory 
* 
# Except this file 
!.gitignore 
!www/.htaccess 

当我运行git status我没有看到的.htaccess为新添加的文件。我曾尝试提交.gitignore文件。依然没有。我错过了明显的东西,不是吗?

更新

我也曾尝试这些:

# Ignore everything in this directory 
* 
*/\.htaccess 

# Except these files 
!.gitignore 
!www/\.htaccess 

# Ignore everything in this directory 
* 
*/.htaccess 

# Except these files 
!.gitignore 
!www/.htaccess 

回答

9

我尝试一些作者我的机器上有足够的步骤,尝试它们并检查它们是否适合您。

cd ~/Desktop/test 
mkdir www && touch .gitignore www/.gitkeep www/.htaccess www/file1 www/file2 
git init && git add www/.gitkeep && git commit -m "gitkeep to make www trackable" 

git status将向您现在

.gitignore 
www/.htaccess 
www/file1 
www/file2 

,在你的.gitignore文件,添加如下两项

www/* 
!www/.htaccess 

做一个git status现在显示

.gitignore 
www/.htaccess 

您现在可以使用git add .添加这两个文件并提交它们。

而且,一旦你确信上述命令的工作,你可以删除www/.gitkeep我在我的例子生成(也有人加入,以保持www可追踪,Git不会版文件夹),并添加www/.htaccess,而不是减少开销的冗余文件。

1

你试过:

*/.htaccess 
!www/.htaccess 
+0

试过。承诺.gitignore,仍然没有看到.htaccess即将到来 – phirschybar

2

以我自己的经验,下面的解决方案比接受的答案简单得多。

以下内容添加到您的.gitignore文件

www/* 
!www/.htaccess 

现在你做!

阐释

要打破它www/*忽视了“www”的目录,但不是目录本身的所有乘客,如在本/www命令。

接下来我们声明我们明确的例外!www/.htaccess,正如您在您的问题中尝试的那样。

它只需要两行在.gitignore文件中,它就像一个魅力。

+0

谢谢,这是问题! 'www'和'!www/.htaccess'意味着Git被告知忽略整个文件夹“www”,所以即使你允许它也不会看到.htaccess文件。通过将第一个语句改为www/*'(意思是“追踪文件夹www本身,但不是其中的任何文件”),它允许第二个语句('!www/.htaccess')找到htaccess文件。要做到这一点,我不得不做相当于'git add www'来开始跟踪文件夹,然后我看到htaccess显示在分段中(并且没有其他文件,就像我打算的那样)。 – buggy3