Question

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

I want to remove a file from my repository.

git rm file_to_remove.txt

will remove the file from the repository, but it will also remove the file from the local file system. How do I remove this file from the repo without deleting my local copy of the file?

 3929  1238693  3929
1 Jan 1970

Solution

 5479

The git rm documentation states:

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 file_to_remove.txt

and for a single directory:

git rm --cached -r directory_to_remove
2009-07-17
bdonlan

Solution

 327

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.

2012-04-04
Sam Tyson

Solution

 313

To remove files from the repository based on .gitignore, without deleting them from the local file system:

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

For Windows Powershell:

git rm --cached $(git ls-files -i -c -X .gitignore)
2014-01-31
null

Solution

 131

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

2015-08-24
Suresh Karia

Solution

 82

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"

2013-12-13
mAsT3RpEE

Solution

 29

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.

2014-01-28
Rystraum

Solution

 26

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
2018-08-15
Afraz Ahmad

Solution

 18

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 rm --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"
2020-04-14
Michael Durrant

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
2016-01-12
Martijn Mellens

Solution

 6

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?

On remote, there is 𝐧𝐨 such thing as an 𝐮𝐧𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞, there is 𝐨𝐧𝐥𝐲 𝐭𝐫𝐚𝐜𝐤𝐞𝐝 𝐬𝐭𝐚𝐭𝐞 and that results in havoc.


Why?

When collaborating with the team, if you 𝐩𝐮𝐬𝐡 such changes up to remote it will delete those changes on remote and all the team who takes a 𝐩𝐮𝐥𝐥 from 𝐫𝐞𝐦𝐨𝐭𝐞 𝐰𝐢𝐥𝐥 𝐜𝐚𝐮𝐬𝐞 𝐝𝐞𝐥𝐞𝐭𝐢𝐨𝐧 𝐨𝐟 𝐭𝐡𝐨𝐬𝐞 𝐟𝐢𝐥𝐞𝐬 𝐰𝐡𝐢𝐜𝐡 𝐲𝐨𝐮 𝐡𝐚𝐯𝐞 𝐣𝐮𝐬𝐭 𝐫𝐞𝐦𝐨𝐯𝐞𝐝 𝐣𝐮𝐬𝐭 𝐟𝐫𝐨𝐦 𝐬𝐭𝐚𝐠𝐞𝐝.

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 𝘨𝘰𝘯𝘦. )

2021-06-01
Zahid Khan

Solution

 2

I used the following simple method to remove some IDE-related files from git as they made the repo look cluttered.

Note: This doesn't remove them from the git history.

Note: If you've accidentially committed passwords the first thing to do is change those passwords.

  • Commit or revert any local changes.
  • Backup the files locally to another folder.
  • Remove the files from your local repo.
  • Commit the change where the files are removed.
  • Edit your .gitignore file to list the files/folders.
  • Copy the files locally back into their original location.
  • Run git status and check that the files are not listed.
  • Commit the change to .gitignore
2023-04-12
James Bradbury

Solution

 1

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.

2020-11-15
Zach

Solution

 -1

If you want to delete a file in the repository but not in the filesystem, it means that you do not want to keep trace of this file. In Git you have 4 alternatives (as specified here).

  1. You can use a .gitignore file. In this case the file/folder is locally untracked.

  2. You can edit .git/info/exclude in a similar way as the previous case. Here the difference is that the file is locally untracked.

  3. You can use the git update-index assume-unchanged <filename> command to track a file, but the local changes are ignored.

  4. You can use git update-index skip-worktree <filename> command to track a file, but the local changes are ignored.

Points 3 and 4 appear to do the same thing, but there are difference. At this link you can find an extensive list of tests about the behavior of these points. To summarize the differences we can say that the flag assume-unchanged assumes that a developer does not touch a file anymore. The flag skip-worktree instructs git to not touch the specific file anymore. It is useful when you add to the repository a configuration file, but you do not want to track its changes.

2023-09-26
Jonny_92