465

I have a folder which I'd like to remove in my remote repository. I'd like to delete it, but keep the folder in my computer

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Rodrigo Souza
  • 7,162
  • 12
  • 41
  • 72
  • 4
    possible duplicate of [Git: Remove a file from the repository without deleting it from the local filesystem](http://stackoverflow.com/questions/1143796/git-remove-a-file-from-the-repository-without-deleting-it-from-the-local-filesys) – Cascabel Aug 12 '10 at 16:36
  • Please look at the suggested related questions as you're writing yours - the duplicate was probably one of the first two. – Cascabel Aug 12 '10 at 16:36
  • Another duplicate: http://stackoverflow.com/questions/1273108/how-do-i-git-rm-a-file-without-deleting-it-from-disk – Cascabel Aug 12 '10 at 16:39
  • 4
    Possible duplicate of [Remove a file from a Git repository without deleting it from the local filesystem](http://stackoverflow.com/questions/1143796/remove-a-file-from-a-git-repository-without-deleting-it-from-the-local-filesyste) – Michael Freidgeim Mar 01 '17 at 00:54

2 Answers2

811
git rm --cached -r somedir

Will stage the deletion of the directory, but doesn't touch anything on disk. This works also for a file, like:

git rm --cached somefile.ext

Afterwards you may want to add somedir/ or somefile.ext to your .gitignore file so that git doesn't try to add it back.

jamessan
  • 41,569
  • 8
  • 85
  • 85
  • 35
    And then add the path to .gitignore so git doesn't try to make you add it later. – grossvogel Aug 12 '10 at 16:24
  • 4
    Will this result in (files in) the directory being removed when he pulls from the remote? – bstpierre Aug 12 '10 at 16:24
  • Not when he pulls; the files will stay removed locally during the pull's automatic merge process. After that, a push will cause the files to be removed server-side. – Walter Mundt Aug 12 '10 at 16:38
  • 8
    What happens if I have a third remote? Will the corresponding files get removed in a future pull? I ask because I feel this is quite a common use case, i.e. check some files into the repo, realise at a later date that they diverge between remotes, for good reason, and should never have been in the repo in the first place, want to resolve that by keeping all local checkouts exactly as they are, but removing the files from the repo. – Bobby Jack Jan 22 '15 at 18:33
  • 8
    For single files, you can do `git rm --cached path/to/file` – Travis Reeder Jan 11 '16 at 22:19
  • @BobbyJack, files will be removed from the third remote upon `git pull` there. (It worked this way for me.) – Jantar88 Feb 18 '17 at 16:43
4

I would just:

  • Move the folder out of your working tree
  • git rm the folder, commit the change
  • Add to .gitignore (or .git/info/excludes), commit the change
  • Move the folder back
Jeff
  • 1,722
  • 13
  • 7