I have an img folder within my git repository. I want to use .gitignore to exclude the images within the folder.
I used cd <file path>
to enter that folder and mkdir .gitignore
. But when I add
the git the images are not excluded. What do I need to add to keep them excluded from the add
?
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 .
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.
. gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.
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:
.gitignore
file in the same directory as the file and the .gitingore
files in their parent directories inside the repo;$GIT_DIR/info/exclude
;~/.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
Your .gitignore
file should be in the repository's root directory to start with. It becomes easier to manage that way. You can then place separate .gitignore
files in the subdirectories where you need to override the patterns defined in the root .gitignore
file.
To exclude the img
directory, simply add this line to .gitignore
:
img/
If you want to make the exclusion case-insensitive, you can instead write:
[Ii]mg/
From the documentation:
If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words,
foo/
will match a directoryfoo
and paths underneath it, but will not match a regular file or a symbolic linkfoo
.
As an aside, you can list the files that are currently ignored in your repository by using the git-ls-files
command:
git ls-files --ignored --others --exclude-standard
where the --exclude-standard
option tells Git to consider the patterns defined in the .git/info/exclude
file, the repository's .gitignore
file (both in the root directory and in all of the subdirectories) as well as the global .gitignore
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With