Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude file only from root folder in Git

Tags:

git

gitignore

I am aware of using .gitignore file to exclude some files being added, but I have several config.php files in source tree and I need to exclude only one, located in the root while other keep under revision control.

What I should write into .gitignore to make this happen?

like image 326
Pavel Karoukin Avatar asked Sep 03 '10 16:09

Pavel Karoukin


People also ask

How do I ignore a specific file in Git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Does .gitignore need to be in root?

A . 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 .

Does Gitignore apply to subdirectories?

gitignore in every subdirectory. This way you can ignore files on a finer grained level if different folders need different rules. Moreover, you can define repository specific rules which are not committed to the Git repository, i.e. these are specific to your local copy. These rules go into the file .


2 Answers

From the documentation:

If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).

A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

So you should add the following line to your root .gitignore:

/config.php 
like image 52
Manoj Govindan Avatar answered Sep 29 '22 16:09

Manoj Govindan


Use /config.php.

like image 33
Richard Fearn Avatar answered Sep 29 '22 16:09

Richard Fearn