Categories
delete-file git git-rm remote-server repository

Remove a file from a Git repository without deleting it from the local filesystem

3559

I want to remove a file from my repository.

git rm file_to_remove.txt

will remove the file from the repository, but it will also remove the file from the local file system. How do I remove this file from the repo without deleting my local copy of the file?

4

5014

The git rm documentation states:

When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.

So, for a single file:

git rm --cached file_to_remove.txt

and for a single directory:

git rm --cached -r directory_to_remove

11

  • 188

    Easily missed because it is not as self explanatory as svn rm --keep-local.

    – Martin

    Jun 24, 2011 at 11:44


  • 130

    But how do I preserve the files on remote servers? This keeps my local one, but if I push and pull from another server, the file gets deleted. I also added a .gitignore for the file, but it still get’s removed

    Feb 22, 2013 at 15:44


  • 12

    This still removes the files on git pull if you are behind the commit after git rm

    May 22, 2014 at 17:02

  • 14

    Worth noting that, after running the command in the answer, you need to use git commit -m "Commit message" and git push. If you have any other staged changes (check with git status), they will also be committed at this time.

    – Sinjai

    Sep 11, 2017 at 20:29


  • 22

    Since this is the most accepted answer and doesn’t do, what is asked for, i’ll tell what I do. I use the command git rm --cached mylogfile.log and delete the file from the repository. To avoid losing the file on productive system i do a backup of the file and pull after this. The file get’s deleted as mentioned before and need to be copied back from your backup. This is quite a pain, but i found no better solution for this problem.

    Dec 14, 2017 at 18:37

305

To remove an entire folder from the repo (like Resharper files), do this:

git rm -r --cached folderName

I had committed some resharper files, and did not want those to persist for other project users.

1

  • 13

    Just an added note for future visitors: Don’t use a GUI to “Sync” the commit back to the repo. That will pull the files back down into your local repo. You have to do a Git Push repo branch to actually remove the files from the remote.

    Dec 4, 2014 at 4:24


246

To remove files from the repository based on .gitignore, without deleting them from the local file system:

git rm --cached `git ls-files -i -X .gitignore`

For Windows Powershell:

git rm --cached $(git ls-files -i -X .gitignore)

8

  • 8

    Does not work on Windows. git ls-files -i -X .gitignore works, but I dont know how to send the files to ‘git rm’. Do you know how to do that?

    – Erik Z

    May 9, 2014 at 6:04

  • 18

    Works on Windows if you use Git Bash instead of cmd-console

    Dec 16, 2014 at 18:12

  • 17

    Love this. Worked except that I had some files that ended up having spaces in filename. I modified solution here to this: git ls-files -i -X .gitignore | xargs -I{} git rm --cached "{}". Please consider modifying or adding this solution to the answer here, because it is a great tool to have…

    – mpettis

    Mar 12, 2016 at 17:48

  • 1

    I didn’t try but it should be tested will it remove also files like .gitkeep which preserves an empty folder in repository. Eg. .gitignore contain folder uploads and repo is forced to keep track of .gitkeep. By removing all from repo under uploads it will remove also .gitkeep.

    Jul 7, 2016 at 9:11

  • 2

    This suggestion worked perfectly in powershell: git rm –cached $(git ls-files -i -X .gitignore)

    Apr 21, 2019 at 11:30