Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 217
phirschybar Avatar asked Oct 18 '13 19:10

phirschybar


People also ask

How do I ignore a file without adding to Gitignore?

To ignore untracked files, you have a file in your git folder called . git/info/exclude . This file is your own gitignore inside your local git folder, which means is not going to be committed or shared with anyone else. You can basically edit this file and stop tracking any (untracked) file.

Can Gitignore ignore folders?

gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .

Should .gitignore be ignored?

The . gitignore file's purpose is to prevent everyone who collaborates on a project from accidentally commiting some common files in a project, such as generated cache files. Therefore you should not ignore .


2 Answers

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.

like image 157
Anshul Goyal Avatar answered Nov 15 '22 05:11

Anshul Goyal


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

Add the following to your .gitignore file

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 /www command would.

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

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

like image 32
Samuel Hawksby-Robinson Avatar answered Nov 15 '22 05:11

Samuel Hawksby-Robinson