Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: how to exclude a subfolder in a repository?

Tags:

git

gitignore

I have created a repository in a folder with my project, you know, git init, I have the Webstorm folder .idea and another one not related to this, I am not interested in keeping in the repository, and I don't want this folder to appear even as new files to add. I want to forget about these subfolders.

  1. How can I exclude this subfolder before committing the whole repository?
  2. How can I exclude this subfolder after committing the whole repository?
like image 242
Mr Question Avatar asked Nov 08 '10 18:11

Mr Question


People also ask

How do I exclude folders from Git repository?

Open the . git/info/exclude file in a text editor and add the folder to ignore. This file remains private to you.

Does Gitignore work in subfolders?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

Can you Gitignore a folder?

A . 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 ignore certain files in Git?

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.


1 Answers

Open the .git/info/exclude file in a text editor and add the folder to ignore. This file remains private to you. You can also create a .gitignore file in the repository's root directory, outside the .git directory and write the folder entry there. Then add this file to the repository to share with others what content should be ignored, should others have this directory-to-ignore in their repository too.

If you already committed this directory then just remove it with git rm -r --cached, commit the deletion and mark the directory ignored as I described above. You might want to make a backup of the directory before you wipe it.

like image 155
wilhelmtell Avatar answered Sep 20 '22 06:09

wilhelmtell