7

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?

Gary Ewan Park
  • 17,610
  • 5
  • 42
  • 60
Imran
  • 120
  • 2
  • 8

4 Answers4

8

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
axiac
  • 68,258
  • 9
  • 99
  • 134
7

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 directory foo and paths underneath it, but will not match a regular file or a symbolic link foo.

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.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • It is possible to place the .gitignore file within the subdirectory, right? It doesn't "need" to be in the root directory? – Gary Ewan Park Aug 19 '16 at 09:08
  • Correct – you can have `.gitignore` files in the subdirectories as well. I didn't say it _has to_ be in the root directory but it becomes easier to manage if you start with that. You can then place other `.gitignore` files in the subdirectories where you need to _override_ the settings defined in the root one. – Enrico Campidoglio Aug 19 '16 at 09:12
  • Perfect, just wanted to check. The wording originally was "you should place" and that is what I wanted to check, – Gary Ewan Park Aug 19 '16 at 09:17
0

You should create a .gitignore file within your git repository (place the file in your root folder - It is easier to manage all your untracked files/folders this way) and add your img folder to that file.

Create a .gitignore file

open the file and add this line:

img/

to the file.

0

You need to edit the .gitignore file in your folder and add * to specify that all files in it should be ignored. Related question

Community
  • 1
  • 1
Leo G.
  • 458
  • 2
  • 10