13

I added a bunch of files to be tracked by git, but they were added (and committed) in error. They should be present in the working directory (they're ephemeral files used by my IDE), but not tracked by git. I've now created a .gitignore file and added the appropriate entries, but what is the proper way to remove the files from being tracked by git while leaving them safely in my working directory?

Matty
  • 33,203
  • 13
  • 65
  • 93
  • Possible duplicate of [Remove file from the repository but keep it locally](http://stackoverflow.com/questions/3469741/remove-file-from-the-repository-but-keep-it-locally) – Michael Freidgeim Mar 01 '17 at 00:53

2 Answers2

21

Use the --cached option:

git rm --cached myfile

Complete explanation here: http://www.kernel.org/pub/software/scm/git/docs/git-rm.html

I also like the --dry-run option. It does not actually change anything, just show what files will be impacted by the real command. It's good to start with that to see what will happen, or to test your file name pattern.

Blacksad
  • 14,906
  • 15
  • 70
  • 81
  • If I do this the deletions are part of my commit. If a colleague then pulls the commit will the files be only removed from their index, or is there a danger it will remove from their working directory too? – dumbledad Nov 09 '16 at 12:53
3

git rm --cached will remove the files from your index but will keep them in your working directory.

Ulrich Dangel
  • 4,515
  • 3
  • 22
  • 30