Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use .gitignore in a folder?

Tags:

git

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?

like image 264
Imran Avatar asked Aug 19 '16 08:08

Imran


People also ask

Does Gitignore work in folder?

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 .

How do I Gitignore a folder?

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.

How do I ignore a folder in Gitignore?

. 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.


2 Answers

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
like image 60
axiac Avatar answered Nov 03 '22 20:11

axiac


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.

like image 38
Enrico Campidoglio Avatar answered Nov 03 '22 20:11

Enrico Campidoglio