Search code examples
gitgitignore

how to make gitignore to ignore some of the folders inside your repo


There are many similar topics on SO and I've read quite a few of them but the problem remained.

I want to be able to put a folder or file name in gitignore so that it doesn't appear in the repo online but remains locally.

What I did:

  1. there is a folder with files locally. I created a new repo using this folder using VSC. I can add and remove files locally, commit, and it will work fine - update the files and file structure.
  2. then I added gitignore hoping to stop tracking some misc folders inside the working dir but after adding said folders it didn't ignore them. There was a suggestion to try git rm -r --catch ., then git add . files back, commit and it would work. It did not.
  3. I tried to git rm -r --catch individually. Strangely it only works on some folders. Not to mention I have to do it for many folders.
  4. finally, as an ultimate solution (for me, I got mad), I just deleted everything locally from that repo leaving only readme and gitignore. SO suggested that one can't simply ignore/stop tracking files that are already in the repo and you have to do some reindexing and what's not but the point is, if the file was added after gitignore, you can ignore it without extensive bash use. Here I just added all the needed files back to that repo locally where only readme and gitignore were. Then I started adding files to gitignore only to find out it wouldn't work. Or should I say some folders would be ignored and some of them wouldn't (top lvl folders inside work dir where gitignore is located). Folders with files, there are no more gitignores inside them or anything like that.

I tried: folder_name, /folder_name, folder_name/, folder_name/**. From 3 folders only 1 would be inored.

If I try creating a new folder with a random name and adding it to ignore the file, it would be ignored.

Before any copy/pasting files back to the local repo folder I deleted the hidden git folder inside it.

This is the picture of my structure ibb.co/F4WrX81 I just wanted to point out that I've used git casually before, more so to get used to it really, and it was all good. Here is an example ibb.co/S5y7LBM I would just create a project, work on it some time, then add gitignore, and everything would go smoothly

Any help or suggestions are much appreciated. I can either redo the whole thing or do some bashing. Don't really care at this point.

Thanks in advance.


Solution

  • Add a filter into .gitignore will not delete matching files/directories. It will make git ignore it on any none specific command. rm command included.

    So do it in multiple steps:

    • Delete (keep a copy if needed) all folders that you want to ignore.
    • Commit the changes in a "Delete the useless folders" commit.
    • Edit .gitignore to add filters for folders to ignore.
    • Commit the changes in a "Update .gitignore file"
    • Restore ignored folders (if any)