Search code examples
git.htaccesscommit

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


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

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 status I don't see .htaccess as being an added file. I have tried committing the .gitignore file. Still nothing. I am missing something obvious, no?

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

Solution

  • 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 status will now show you

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

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

    www/*
    !www/.htaccess
    

    Doing a git status now shows

    .gitignore
    www/.htaccess
    

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

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