Remove a file from a Git repository without deleting it from the local filesystem
GitRepositoryRemote ServerDelete FileGit RmGit Problem Overview
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?
Git Solutions
Solution 1 - Git
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
Solution 2 - Git
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 - Git
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 - Git
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
Solution 5 - Git
A more generic solution:
-
Edit
.gitignore
file.echo mylogfile.log >> .gitignore
-
Remove all items from index.
git rm -r -f --cached .
-
Rebuild index.
git add .
-
Make new commit
git commit -m "Removed mylogfile.log"
Solution 6 - Git
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 7 - Git
> 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 - Git
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
Solution 9 - Git
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"
Solution 10 - Git
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 11 - Git
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).
- And have not pushed to the remote.
- 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.
On remote, there is 퐧퐨 such thing as an 퐮퐧퐭퐫퐚퐜퐤퐞퐝 퐬퐭퐚퐭퐞, there is 퐨퐧퐥퐲 퐭퐫퐚퐜퐤퐞퐝 퐬퐭퐚퐭퐞 and that results in havoc. What could go wrong?
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 퐫퐞퐦퐨퐭퐞 퐰퐢퐥퐥 퐜퐚퐮퐬퐞 퐝퐞퐥퐞퐭퐢퐨퐧 퐨퐟 퐭퐡퐨퐬퐞 퐟퐢퐥퐞퐬 퐰퐡퐢퐜퐡 퐲퐨퐮 퐡퐚퐯퐞 퐣퐮퐬퐭 퐫퐞퐦퐨퐯퐞퐝 퐣퐮퐬퐭 퐟퐫퐨퐦 퐬퐭퐚퐠퐞퐝. 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 혨혰혯혦. )
Solution 12 - Git
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.