A gitignore file is used to specify files that you want Git to intentionally ignore. Using gitignore has several advantages, the main one advantage being that you can prevent sensitive information, such as database credentials or other configuration files, from being accidentally committed.
.gitignore file is usually placed in the working directory, or the root of your project.
For example, let’s assume that we have this basic project structure and a
.git directory has just been initialised within the project.
.git/ app/ .htaccess App.php css/ styles.css ie.css config/ db.php .gitignore .htaccess index.html index.php
When running a
git status, Git shows all the files untracked.
$ git status -u On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore .htaccess app/.htaccess app/App.php config/db.php css/ie.css css/styles.css index.html index.php
Open up the
.gitignore file and add in the
index.html files separated by a new line.
// .gitignore .htaccess config/db.php index.html
git status -u, and you’ll notice that the index.html, db.php and both .htaccess files are ignored.
Untracked files: (use "git add
..." to include in what will be committed) .gitignore app/App.php css/ie.css css/styles.css index.php
To ignore the
.htaccess in the root directory but keep the one in the
app/ directory, add a forward slash before the file name in
// .gitignore /.htaccess config/db.php index.html
This following is stated in Git’s 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).”
Whilst this example only ignores 3 files, larger projects may require more information in the gitignore file. For larger gitignore files, you can help keep the file readable by adding comments using a hash,
// .gitignore # A comment about ignoring base project files /.htaccess index.html # ...and the database credentials! config/db.php
You can also use an optional prefix, represented by an exclamation mark,
!, which, according to the documentation:
“negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources.”
This means that if you want to ignore an entire directory and the files/directories within it, but keep one file, you could add the following into
// .gitignore # A comment about ignoring base project files /.htaccess index.html # ...and the database credentials! config/db.php # Ignore all in app/ but not the .htaccess! app/* !app/.htaccess
It should be noted that files already tracked by Git are not affected. So if you had a file into gitignore that has already been committed, you will need to remove the file from Git by running
git rm --cached path/to/ignored/file.php
A useful command to show current ignored files is using
git status but passing in the
$ git status --ignored