572

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?

Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
Pavel Karoukin
  • 5,893
  • 2
  • 16
  • 9

5 Answers5

815

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
Community
  • 1
  • 1
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
  • 2
    Thank you! I tried it this way, but for some reason it didn't worked. Probably mistyped somewhere something =) – Pavel Karoukin Sep 03 '10 at 18:03
  • 2
    What if it's not a file, but a folder, and I want to ignore that folder in repo root, its contained files and all descendent subfolders and their files? `/folder/`? – CodeManX Aug 23 '15 at 20:31
  • 28
    Either `/folder/` or `/folder` would work, but adding the slash at the ends limits the match to folders only. If you had a file named 'foo' in the root directory, `/foo/` would not ignore it, but `/foo` would. – tehDorf Jun 20 '17 at 00:19
  • 4
    In case you already commited the file, run command `git rm --cached `, else the file won't get ignored. From: https://stackoverflow.com/a/1274447 – Bruno Polo May 26 '20 at 20:24
  • After change .gitignore file, run command `git rm --cached `! this worked for me. – TangHongWan Jan 30 '22 at 02:27
107

Use /config.php.

Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
44

Older versions of git require you first define an ignore pattern and immediately (on the next line) define the exclusion. [tested on version 1.9.3 (Apple Git-50)]

/config.php
!/*/config.php

Later versions only require the following [tested on version 2.2.1]

/config.php
kenhowardpdx
  • 812
  • 1
  • 11
  • 15
  • That first syntax is also very useful if you want to ignore a file at the top level of your project but not at lower levels. – light24bulbs May 30 '22 at 19:22
26

If the above solution does not work for you, try this:

#1.1 Do NOT ignore file pattern in  any subdirectory
!*/config.php
#1.2 ...only ignore it in the current directory
/config.php

##########################

# 2.1 Ignore file pattern everywhere
config.php
# 2.2 ...but NOT in the current directory
!/config.php
drugan
  • 789
  • 9
  • 10
  • This works for me indeed, whereas the `/config.php` alone did not. I am curious why it did not. Do you have an idea? – iago-lito Jan 30 '19 at 16:03
  • @iago-lito if you work on some particular project and struggling on that how to ignore some file.ext in some particular directory and at the same time to NOT ignore it everywhere else then put this into /home/me/.gitignore file: /home/me/path/to/my/project/some/folder/file.ext OR file.ext to ignore the file everywhere and then in the /home/me/path/to/my/project/some/folder/.gitignore file put this !file.ext to NOT ignore this file just in this particular directory. Each folder can have its own .gitignore file to OVERRIDE any of the parent .gitignore file(s) settings... – drugan Feb 01 '19 at 06:52
  • Hm. This means that the repo may behave differently depending on whether other users have different `~/.gitignore` files on their own machines, right?.. Besides, my point was: You wrote "If the above solution does not work", why would it not? – iago-lito Feb 01 '19 at 09:42
  • yes, if your project may exist in unknown environment then you should never rely on .gitignore files which exist above your project root folder. Remember that file name like config.php are quite common so override any possibly existing setting for this file in your root .gitignore file. – drugan Feb 02 '19 at 10:09
  • @iago-lito'consideringleaving see https://stackoverflow.com/a/28000594/362021 – Malcolm Feb 28 '21 at 19:46
  • @drugan answers in SO can change their ordering without notice based on ranking, and can also be re-ordered (unlike a forum where the ordering is fixed chronologically). Instead of saying "above" or "below," directly refer to the answer by a link and/or the author's name. – ADTC Jul 12 '22 at 07:40
0

An example for a wordpress site, but basically ignore everything and then add exceptions starting with ! for what to include

# Ignore everything in the root except the "wp-content" directory.
/*
!.gitignore
!wp-content/
!wp-config.php
#
# # Ignore everything in the "wp-content" directory, except the "plugins"
# # and "themes" directories.
wp-content/*
!wp-content/plugins/
!wp-content/themes/
#
# # Ignore everything in the "plugins" directory, except the plugins you
# # specify (see the commented-out examples for hints on how to do this.)
wp-content/plugins/*
# # !wp-content/plugins/my-single-file-plugin.php
# # !wp-content/plugins/my-directory-plugin/
#
# # Ignore everything in the "themes" directory, except the themes you
# # specify (see the commented-out example for a hint on how to do this.)
wp-content/themes/*
!wp-content/themes/twentyeleven/
  • Instead of having `!wp-content/` then `wp-content/*` then `!wp-content/themes/` then `wp-content/themes/*` then `!wp-content/themes/twentyeleven/` you could just have one line `!wp-content/themes/twentyeleven/` only to the same effect. Couldn't you? – ADTC Jul 12 '22 at 07:37