git 让 .gitignore 不忽略顶级目录中的 .htaccess 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19457301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:14:20  来源:igfitidea点击:

Have .gitignore to NOT ignore an .htaccess file within top level directory

git.htaccesscommit

提问by phirschybar

I am trying to ignore ALL files except the .htaccess file within the www directory.

我试图忽略 www 目录中除 .htaccess 文件之外的所有文件。

I have this:

我有这个:

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

And I have also tried this:

我也试过这个:

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

When I run git statusI don't see .htaccess as being an added file. I have tried committing the .gitignore file. Still nothing. I am missing something obvious, no?

当我运行时,git status我没有将 .htaccess 视为添加的文件。我试过提交 .gitignore 文件。依然没有。我错过了一些明显的东西,不是吗?

Update

更新

I have also tried these:

我也试过这些:

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

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

and

# Ignore everything in this directory
*
*/.htaccess

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

回答by u1860929

I tried some sample steps on my machine, try them and check if they work for you.

我在我的机器上尝试了一些示例步骤,尝试它们并检查它们是否适合您。

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"

Doing git statuswill now show you

git status现在会告诉你

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

Now, In your .gitignore file, add the following two entries

现在,在您的 .gitignore 文件中,添加以下两个条目

www/*
!www/.htaccess

Doing a git statusnow shows

做一个git status现在显示

.gitignore
www/.htaccess

You can now add these two files using git add .and commit them.

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

Also, once you are convinced that the said commands work, you can remove the www/.gitkeepI generate in my example (It was added to keep wwwtrackable, git doesn't version folders) and add www/.htaccessinstead to reduce the overhead of redundant files.

此外,一旦您确信上述命令有效,您可以删除www/.gitkeep我在我的示例中生成的(添加它是为了保持www可跟踪,git 不版本文件夹)并添加www/.htaccess以减少冗余文件的开销。

回答by Samuel Hawksby-Robinson

In my own experience the below solution is much more simple than the accepted answer.

根据我自己的经验,以下解决方案比公认的答案简单得多。

Add the following to your .gitignore file

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

www/*
!www/.htaccess

Now you are done!

现在你完成了!

The Explanation

说明

To break it down www/*ignores all occupants of the "www" directory but not the directory itself, as the /wwwcommand would.

分解它会www/*忽略“www”目录的所有占用者,而不是目录本身,就像/www命令一样。

Next we declare our explicit exception !www/.htaccessas you've attempted in your question.

接下来,!www/.htaccess正如您在问题中所尝试的那样,我们声明我们的显式异常。

It only takes two lines in the .gitignore file and it works like a charm.

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

回答by Jon Lin

Have you tried:

你有没有尝试过:

*/.htaccess
!www/.htaccess