0

How can I delete a file from git if it is inside a folder marked to be ignored by git. For example - /target/some_old_file.txt was pushed to git before target was added in .gitignore

is it possible without modifying the .gitignore file.

Ashish
  • 14,295
  • 21
  • 82
  • 127
  • do you mean delete from git repo or delete from file system? – priteshbaviskar Aug 02 '18 at 18:21
  • delete it from git repo, – Ashish Aug 02 '18 at 18:43
  • Possible duplicate of [How to make Git "forget" about a file that was tracked but is now in .gitignore?](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore) – phd Aug 02 '18 at 18:49

2 Answers2

1

To delete a file from git repo, you need to use

git rm --cached path/to/your/file

This means that git essentially forgets about the file in question. Please make sure to use --cached flag. Without the flag, the file will also be deleted from file system.

priteshbaviskar
  • 2,139
  • 2
  • 20
  • 27
0
  1. Remove the target folder from .gitignore
  2. Remove the file and commit
  3. Change the .gitignore to ignore the target folder again.
Ashu
  • 2,066
  • 3
  • 19
  • 33