3327
votes

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?

12
Duplicate of Stop tracking and ignore changes to a file in Git โ€“ user456814
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

12 Answers

4741
votes

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
299
votes

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.

231
votes

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)
109
votes

As per my Answer here: https://stackguides.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

74
votes

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"

73
votes

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/

26
votes

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.

24
votes

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

git update-index --assume-unchanged  file_name_with_path
8
votes

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
0
votes

This depends on what you mean by 'remove' from git. :)

You can unstage a file using git rm --cached see for more details. When you unstage something, it means that it is no longer tracked, but this does not remove the file from previous commits.

If you want to do more than unstage the file, for example to remove sensitive data from all previous commits you will want to look into filtering the branch using tools like the BFG Repo-Cleaner.

0
votes

I would like to add to the accepted answer of @bdonlan.


DON'T USE THIS ANSWER TO REMOVE FILE(S) THAT EXISTS ON REMOTE.

git rm --cached filename

What the answer is supposed to do?

It is supposed to remove some files from the local staged area that you have mistakenly committed in some previous commit(s).

  1. And have not pushed to the remote.
  2. And if pushed on remote, others don't care about those changes.

It moves files from Tracked ๐ญ๐จ Untracked state by that what I mean is, it deletes the files and adds them again.

So, git doesn't know about them anymore.


What could go wrong?




Why?



Summary: You removed files from staged and then pushed them will result in the deletion of files on the collaborating team's local repository as well (๐˜ธ๐˜ฉ๐˜ช๐˜ญ๐˜ฆ ๐˜บ๐˜ฐ๐˜ถ ๐˜ฉ๐˜ข๐˜ท๐˜ฆ ๐˜ต๐˜ฉ๐˜ฐ๐˜ด๐˜ฆ ๐˜ง๐˜ช๐˜ญ๐˜ฆ๐˜ด ๐˜ข๐˜ท๐˜ข๐˜ช๐˜ญ๐˜ข๐˜ฃ๐˜ญ๐˜ฆ ๐˜ช๐˜ฏ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ถ๐˜ฏ๐˜ต๐˜ณ๐˜ข๐˜ค๐˜ฌ๐˜ฆ๐˜ฅ ๐˜ด๐˜ต๐˜ข๐˜จ๐˜ฆ, ๐˜ง๐˜ฐ๐˜ณ ๐˜ข๐˜ญ๐˜ญ ๐˜ต๐˜ฆ๐˜ข๐˜ฎ๐˜ด ๐˜ช๐˜ต will be ๐˜จ๐˜ฐ๐˜ฏ๐˜ฆ. )

-1
votes

Ignore the files, remove the files from git, update git (for the removal).

Note : this does not deal with history for sensitive information.

This process definitely takes some undertanding of what is going on with git. Over time, having gained that, I've learned to do processes such as:

1) Ignore the files

  • Add or update the project .gitignore to ignore them - in many cases such as yours, the parent directory, e.g. log/ will be the regex to use.
  • commit and push that .gitignore file change (not sure if push needed mind you, no harm if done).

2) Remove the files from git (only).

  • Now remove the files from git (only) with git remove --cached some_dir/
  • Check that they still remain locally (they should!).

3) Add and commit that change (essentially this is a change to "add" deleting stuff, despite the otherwise confusing "add" command!)

  • git add .
  • git commit -m"removal"