从 Git 存储库中删除文件而不从本地文件系统中删除它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1143796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove a file from a Git repository without deleting it from the local filesystem
提问by mveerman
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.
我的初始提交包含一些日志文件。我已经添加*log
到我的.gitignore
,现在我想从我的存储库中删除日志文件。
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 withoutdeleting my local copy of the file?
如何从存储库中删除此文件而不删除该文件的本地副本?
回答by bdonlan
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.
当
--cached
给出时,暂存内容必须匹配分支的尖端或磁盘上的文件,允许仅从索引中删除文件。
So, for a single file:
因此,对于单个文件:
git rm --cached mylogfile.log
and for a single directory:
对于单个目录:
git rm --cached -r mydirectory
回答by Sam Tyson
To remove an entire folder from the repo (like Resharper files), do this:
要从存储库中删除整个文件夹(如 Resharper 文件),请执行以下操作:
git rm -r --cached folderName
I had committed some resharper files, and did not want those to persist for other project users.
我已经提交了一些 resharper 文件,并且不希望这些文件为其他项目用户保留。
回答by null
You can also remove files from the repository based on your .gitignore without deleting them from the local file system :
您还可以根据您的 .gitignore 从存储库中删除文件,而无需从本地文件系统中删除它们:
git rm --cached `git ls-files -i -X .gitignore`
Or, alternatively, on Windows Powershell:
或者,在 Windows Powershell 上:
git rm --cached $(git ls-files -i -X .gitignore)
回答by eirenaios
As per my Answer here: https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in-my-github-repository
根据我的回答: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.
要仅从 git 存储库而不是从本地删除文件夹/目录或文件,请尝试 3 个简单的步骤。
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 .gitignoreand put that folders name into it. You can put as many as you want
要从下一次提交中忽略该文件夹,请在根目录中创建一个名为.gitignore 的文件 ,并将该文件夹名称放入其中。你可以放任意多
.gitignorefile will be look like this
.gitignore文件看起来像这样
/FolderName
回答by BoD
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/
此外,如果您提交了敏感数据(例如包含密码的文件),您应该将其从存储库的历史记录中完全删除。这是一个解释如何做到这一点的指南:http: //help.github.com/remove-sensitive-data/
回答by mAsT3RpEE
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"
编辑
.gitignore
文件。ECHO mylogfile.log >> .gitignore
从索引中删除所有项目。
git rm -r -f --cached .
重建索引。
git add .
进行新的提交
git commit -m "Removed mylogfile.log"
回答by Rystraum
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.
Git 允许您通过假设它们未更改来忽略这些文件。这是通过运行
git update-index --assume-unchanged path/to/file.txt
命令来完成的。一旦将文件标记为这样,git 将完全忽略对该文件的任何更改;它们不会在运行 git status 或 git diff 时出现,也不会被提交。
(From https://help.github.com/articles/ignoring-files)
(来自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.
注意:这不是直接回答问题,而是基于其他答案评论中的后续问题。
回答by Afraz Ahmad
If you want to just untrack a file and not delete from local and remote repo then use thhis command:
如果您只想取消跟踪文件而不是从本地和远程存储库中删除,请使用以下命令:
git update-index --assume-unchanged file_name_with_path
回答by Martijn Mellens
Above answers didn't work for me. I used filter-branch
to remove all committed files.
以上答案对我不起作用。我曾经filter-branch
删除所有提交的文件。
Remove a file from a git repository with:
使用以下命令从 git 存储库中删除文件:
git filter-branch --tree-filter 'rm file'
Remove a folder from a git repository with:
使用以下命令从 git 存储库中删除文件夹:
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
回答by Michael Durrant
Ignore the files, remove the files from git, update git (for the removal).
忽略文件,从 git 中删除文件,更新 git(用于删除)。
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:
这个过程肯定需要对 git 正在发生的事情有所了解。随着时间的推移,获得了这一点,我学会了执行以下过程:
1) Ignore the files
1)忽略文件
- 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).
- 添加或更新项目
.gitignore
以忽略它们 - 在许多情况下,例如您的父目录,例如log/
将是要使用的正则表达式。 - 提交并推送该
.gitignore
文件更改(不确定是否需要推送,请注意,如果完成没有害处)。
2) Remove the files from git (only).
2)从git中删除文件(仅限)。
- Now remove the files from git (only) with
git remove --cached some_dir/
- Check that they still remain locally (they should!).
- 现在从 git (仅)中删除文件
git remove --cached some_dir/
- 检查它们是否仍然保留在本地(它们应该!)。
3) Add and commit that change (essentially this is a change to "add" deleting stuff, despite the otherwise confusing "add" command!)
3)添加并提交该更改(本质上这是对“添加”删除内容的更改,尽管“添加”命令在其他方面令人困惑!)
git add .
git commit -m"removal"
git add .
git commit -m"removal"