gitgitignore

.gitignore: Is it possible to use regex pattern to match at least one more character


My website's .htaccess was not in git, I have a rule to ignore it:

.htaccess*  

Because I have these files

.htaccess
.htaccess.local
.htaccess.online
.htaccess.testsite

So I use * in .gitignore. Besides, sometimes

.htaccess_local.txt
.htaccess_online.txt
...

So, one * can do this.

Now I would like to add .htaccess, but still ignore others. I tried

.htaccess(.)+

It seems not working.


Solution

  • .htaccess(.)+ doesn't work because .gitignore doesn't use regular expression syntax; it uses shell wildcard (fnmatch) syntax.

    I see 2 ways to fix your problem. The simplest is to continue ignoring .htaccess* but force add one file (forced adding allows to add ignored files):

    git add --force .htaccess
    

    Tracked files are never ignored even if they match .gitignore patterns.

    The second way if to ignore all files and unignore the one:

    .htaccess*
    !.htaccess