The .gitignore
file does not have any effect only by its mere presence. An empty .gitignore
file is just an empty file, nothing more.
Git
reads the content of the .gitignore
file and uses the patterns it contains to decide what files to put in the repo and what files to ignore. Read more about the format of .gitignore
files.
Contrary to what other answers to this question state, you can have a .gitignore
file in any directory of your repository, not only in the root directory.
When it decides how to handle a new file (track it or ignore it) it combines the information from multiple gitignore
sources:
- the command line;
- the
.gitignore
file in the same directory as the file and the .gitingore
files in their parent directories inside the repo;
- the local repository ignore file (not pushed to the remote repos)
$GIT_DIR/info/exclude
;
- the global ignore file specified in
~/.gitconfig
; it applies to all local repository of the user.
Back to your concrete situation, if you want to tell Git
to ignore the files in the img
directory you can put *
in the file img/.gitignore
.
Another option is to put <path-to-img>/*
in the .gitignore
file on the root directory of your repo. Replace <path-to-img>
with the actual path from the root of the repo to the img
file.
The content of the file .gitignore
is used only by git add
. If a file is already in the repository, adding in .gitignore
a pattern that matches its name doesn't make Git
ignore it from now on. It must be manually removed from the repo (and the removal committed), otherwise Git
will continue to track its content.
In your situation, the commands to run are along these lines:
cd <path-to-img-directory>
echo "*" >> .gitignore
git rm *
git add .gitignore
git commit