Remove file from the repository but keep it locally

GitGitignoreGit Rm

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRodrigo SouzaView Question on Stackoverflow
Solution 1 - GitjamessanView Answer on Stackoverflow
Solution 2 - GitJeffView Answer on Stackoverflow