0

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!

phd
  • 82,685
  • 13
  • 120
  • 165
Tometoyou
  • 7,792
  • 12
  • 62
  • 108
  • Rename the folder. (Back it up somewhere temporarily, if you like.) `git rm Design`. Rename it back. – ceejayoz Mar 08 '19 at 14:27
  • @ceejayoz When I did that it said `fatal: not removing 'Design' recursively without -r` – Tometoyou Mar 08 '19 at 14:28
  • That error message seems pretty clear. Add the `-r`? – ceejayoz Mar 08 '19 at 14:36
  • Possible duplicate of [gitignore after commit](https://stackoverflow.com/questions/6535362/gitignore-after-commit) – Kapcash Mar 08 '19 at 14:38
  • @ceejayoz Yep worked, thanks, if you put that as an answer I'll accept. I was just cautious because I've tried a few answers on here and bad things happened haha. – Tometoyou Mar 08 '19 at 14:41
  • 1
    @ceejayoz There's the `--cached` flag, which removes the file from the index while keeping it in the working tree, i.e. `git rm --cached -r Design`. – alfunx Mar 08 '19 at 15:34
  • 1
    Possible duplicate of [Remove directory from remote repository after adding them to .gitignore](https://stackoverflow.com/questions/7927230/remove-directory-from-remote-repository-after-adding-them-to-gitignore) – phd Mar 08 '19 at 16:00
  • 1
    https://stackoverflow.com/search?q=%5Bgitignore%5D+after+add – phd Mar 08 '19 at 16:00

1 Answers1

1

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

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52