Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
1  
Duplicate of Stop tracking and ignore changes to a file in Git – user456814 May 24 '14 at 23:21
up vote 2427 down vote accepted
git rm --cached mylogfile.log

For a directory:

git rm --cached -r mydirectory
share|improve this answer
6  
Thanks! I'm not sure how I missed this in man ... "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." – mveerman Jul 17 '09 at 15:15
77  
maybe because it is not as self explanatory as --keep-local. – Martin Jun 24 '11 at 11:44
16  
@DGGenuine, --keep-local is an argument to svn rm – bdonlan Oct 20 '11 at 21:30
8  
@bdonian thanks. Must be an option to new git-rm version. Ironically this page is the top result when googling "git rm '--keep-local'" – Carl G Oct 26 '11 at 4:33
64  
But how o 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 – spankmaster79 Feb 22 '13 at 15:44

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.

share|improve this answer
6  
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. – RubberDuck Dec 4 '14 at 4:24

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`
share|improve this answer
7  
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 '14 at 6:04
10  
Works on Windows if you use Git Bash instead of cmd-console – Andreas Zita Dec 16 '14 at 18:12
4  
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 at 17:48
    
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. – mrW Jul 7 at 9:11

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/

share|improve this answer

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"

share|improve this answer
1  
Will this actually delete the file ? – Mr_and_Mrs_D Dec 13 '13 at 15:39
4  
From GitHub? NO. If you have already pushed to github it will not remove it from the site. But it will update your local git repository. – mAsT3RpEE Dec 14 '13 at 12:37
    
I think you want this: help.github.com/articles/remove-sensitive-data – mAsT3RpEE Dec 14 '13 at 12:38
2  
The comment you deleted was more eliminated actually :) The problem with the solution rm --cashed is that it will eventually delete the file when one pulls - right ? And this is not what people want when they say "Remove a file from the repository without deleting it from the local filesystem". Now why was the solution above accepted is beyond me - probably the OP was working alone and never pulled ? Dunno. I understand the github "once pushed always there" issue ofc – Mr_and_Mrs_D Dec 14 '13 at 12:42
3  
My problem is not github - it is that the file will actually be deleted from coworkers when they pull. I don't want the file to be deleted. This has caused me huge issues in the past. So I was wondering if there is really a solution that really does not delete the file. See also the comment inn the accepted answer : stackoverflow.com/questions/1143796/… – Mr_and_Mrs_D Dec 14 '13 at 13:07

As per my Answer here: 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

share|improve this answer
2  
too bad everyone didn't answer questions like this -- regardless of how good an answer is, nothing beats a good colorful visual aid!!! :-) – osirisgothra Sep 11 '15 at 17:22
    
@osirisgothra Yeah right man, thanks – eirenaios May 30 at 7:13
    
Out of curiosity, how did you do the blurring? – kmort Nov 3 at 21:48
    
@kmort Haha, I did it using Google Chrome browser's "Awesome Screenshot" tool, was really quick that's why looking messy. Sorry for that!! awesomescreenshot.com – eirenaios Nov 4 at 6:37

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.

share|improve this answer
2  
Hm, I do this but I see that the file can be overwritten if someone else makes changes to it on the repo. – AlxVallejo Sep 15 '14 at 16:50
    
This answer combined with Martijn's are what I needed to delete it without future pulls overwriting files for someone else or myself. – notbad.jpeg Mar 17 at 20:07

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
share|improve this answer
1  
This combined with git rm -r --cached NAME is the trick to remove it from your local git repo and prevent it from affecting anyone who pulls later (by deleting history of the file or directory from git.) – notbad.jpeg Mar 17 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.