Search code examples
gitgitignore

How to remove cached/tracked files after adding a gitignore?


I have a folder in my repository called Design. I have since added /Design/ to my .gitignore file. However, the old files are still tracked and appear in bitbucket. How can I remove these cached files from my online repository but keep them on my computer? I tried something in the past but ended up deleting them from my computer, and I don't want that to happen again!


Solution

  • If you just mean to remove the files from the repo going forward, the simplest way is

    git rm --cached -r Design
    

    to update the index, and then when you commit the new commit will omit the directory.

    Backing the files up, then doing

    git rm -r Design
    

    and then copying the files back (per comments) also works, but is unnecessarily complicated since that's exactly what the --cached option is for.

    If you mean to purge the Design folder from the repo entirely, this is more involved and requires a history rewrite (which in turn has some costs you would need to read up about). You can find plenty of questions and answers that outline all of this; you'd be looking for git filter-branch info