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

ID : 143

viewed : 51

Tags : gitrepositoryremote-serverdelete-filegit-rmgit

Top 5 Answer for Remove a file from a Git repository without deleting it from the local filesystem

vote vote

92

From the man file:

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 mylogfile.log 

and for a single directory:

git rm --cached -r mydirectory 
vote vote

86

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.

vote vote

77

You can also remove files from the repository based on your .gitignore without deleting them from the local file system :

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

Or, alternatively, on Windows Powershell:

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

64

As per my Answer here: https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in-my-github-repository

To remove folder/directory or file only from git repository and not from the local try 3 simple steps.


Steps to remove directory

git rm -r --cached File-or-FolderName git commit -m "Removed folder from repository" git push origin master 

Steps to ignore that folder in next commits

To ignore that folder from next commits make one file in root named .gitignore and put that folders name into it. You can put as many as you want

.gitignore file will be look like this

/FolderName 

remove directory

vote vote

54

Also, if you have commited sensitive data (e.g. a file containing passwords), you should completely delete it from the history of the repository. Here's a guide explaining how to do that: http://help.github.com/remove-sensitive-data/

Top 3 video Explaining Remove a file from a Git repository without deleting it from the local filesystem

Related QUESTION?