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



Question:

My initial commit contained some log files. I've added *log to my .gitignore, and now I want to remove the log files from my repository.

git rm mylogfile.log  

will remove a file from the repository, but will also remove it from the local file system.

How can I remove this file from the repo without deleting my local copy of the file?


Solution:1

For single file:

git rm --cached mylogfile.log  

For single directory:

git rm --cached -r mydirectory  


Solution:2

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.


Solution:3

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)  


Solution:4

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/


Solution:5

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


Solution:6

A more generic solution:

  1. Edit .gitignore file.

    ECHO mylogfile.log >> .gitignore

  2. Remove all items from index.

    git rm -r -f --cached .

  3. Rebuild index.

    git add .

  4. Make new commit

    git commit -m "Removed mylogfile.log"


Solution:7

Git lets you ignore those files by assuming they are unchanged. This is done by running the git update-index --assume-unchanged path/to/file.txt command. Once marking a file as such, git will completely ignore any changes on that file; they will not show up when running git status or git diff, nor will they ever be committed.

(From https://help.github.com/articles/ignoring-files)

Hence, not deleting it, but ignoring changes to it forever. I think this only works locally, so co-workers can still see changes to it unless they run the same command as above. (Still need to verify this though.)

Note: This isn't answering the question directly, but is based on follow up questions in the comments of the other answers.


Solution:8

Above answers didn't work for me. I used filter-branch to remove all committed files

remove a file from a git repository with:

git filter-branch --tree-filter 'rm  file'  

remove a folder from a git repository with:

git filter-branch --tree-filter 'rm -rf directory'  

This removes the directory or file from all the commits

you can specify a commit by using:

git filter-branch --tree-filter 'rm -rf directory' HEAD  

Or an range

git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD  

To push everything to remote, you can do:

git push origin master --force  


Solution:9

If you want to just untrack a file and not delete from local and remote repo then use thhis command:

git update-index --assume-unchanged  file_name_with_path  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »