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?

  • 4
    Duplicate of Stop tracking and ignore changes to a file in Git – user456814 May 24 '14 at 23:21
  • It's worth noting that the most upvoted answer is dangerous for some. If you are using a remote repo than when you push your local then pull elsewhere those files you removed from git only WILL BE DELETED. This is mentioned in one of the replies but not commented upon. – RichieHH Aug 24 at 4:17
up vote 3546 down vote accepted

For single file:

git rm --cached mylogfile.log

For single directory:

git rm --cached -r mydirectory
  • 12
    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
  • 112
    maybe because it is not as self explanatory as --keep-local. – Martin Jun 24 '11 at 11:44
  • 25
    @DGGenuine, --keep-local is an argument to svn rm – bdonlan Oct 20 '11 at 21:30
  • 11
    @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
  • 83
    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.

  • 12
    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
  • 1
    @RubberDuck An answer 3 years old, and the only solution that worked for me :) Thanks – Mariusz Mar 25 at 3:30

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)
  • 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
  • 15
    Works on Windows if you use Git Bash instead of cmd-console – Andreas Zita Dec 16 '14 at 18:12
  • 11
    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 '16 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. – Vladimir Vukanac Jul 7 '16 at 9:11
  • Note: does not work with nested .gitignore files – acidjunk Nov 13 '17 at 7:23

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/

  • 6
    This answer should include the required commands to complete this task instead of linking to another website. – Florian Lemaitre Feb 2 at 13:08

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

  • Out of curiosity, how did you do the blurring? – kmort Nov 3 '16 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 '16 at 6:37

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"

  • 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
  • 5
    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
  • 7
    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

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.

  • 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

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
  • 2
    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 '16 at 20:15
  • 1
    This rewrites git history and you would need to push --force after, this is a bit out of scope with the question I guess. On a public famous repo you can't just change the history line like that as everyone having already cloned the repo would get issues when pulling. – Guillaume Perrot Apr 1 '17 at 1:37
  • Thanks, finally someone is explaining the good old step by step method without any external tools – realtebo Jun 29 at 19:01

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

git update-index --assume-unchanged  file_name_with_path

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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