从 Git 存储库中删除文件,而不将其从本地文件系统中删除
我想从我的存储库中删除一个文件。
git rm file_to_remove.txt
将从存储库中删除该文件,但也会从本地文件系统中删除该文件。 如何从存储库中删除此文件而不删除该文件的本地副本?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
git rm
文档指出:因此,对于单个文件:
和单个目录:
The
git rm
documentation states:So, for a single file:
and for a single directory:
要从存储库中删除整个文件夹(如 Resharper 文件),请执行以下操作:
我已提交一些 resharper 文件,并且不希望其他项目用户保留这些文件。
To remove an entire folder from the repo (like Resharper files), do this:
I had committed some resharper files, and did not want those to persist for other project users.
要从基于
.gitignore
的存储库中删除文件,而不从本地文件系统中删除它们:对于 Windows Powershell:
To remove files from the repository based on
.gitignore
, without deleting them from the local file system:For Windows Powershell:
根据我的回答:https://stackoverflow.com/questions/6313126/how-to-remove-a-directory-in- my-github-repository
要仅从 git 存储库而不是从本地删除文件夹/目录或文件,请尝试 3 个简单的步骤。
删除目录的步骤
在下次提交中忽略该文件夹的步骤
.gitignore 文件,如下所示
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
Steps to ignore that folder in next commits
.gitignore file will be look like this
更通用的解决方案:
编辑
.gitignore
文件。echo mylogfile.log>> .gitignore
从索引中删除所有项目。
git rm -r -f --cached 。
重建索引。
git add .
进行新提交
git commit -m“已删除 mylogfile.log”
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"
另外,如果您提交了敏感数据(例如包含密码的文件),您应该将其从存储库的历史记录中完全删除。 这是解释如何执行此操作的指南:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository
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:
https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository
(来自 https://help.github.com/articles/ignoring-files)
因此,不要删除它,但永远忽略它的变化。
我认为这只适用于本地,因此同事仍然可以看到它的更改,除非他们运行与上面相同的命令。 (但仍然需要验证这一点。)
注意:这不是直接回答问题,而是基于其他答案的评论中的后续问题。
(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.
如果您只想取消跟踪文件并且不删除本地和远程存储库,请使用以下命令:
If you want to just untrack a file and not delete from local and remote repo then use this command:
忽略文件,从 git 中删除文件,更新 git (用于删除)。
注意:这不处理敏感信息的历史记录。
这个过程肯定需要对 git 的运行情况有一定的了解。 随着时间的推移,我学会了执行以下过程:
1)忽略文件
.gitignore
以忽略它们 - 在许多情况下,例如您的情况,父目录,例如log/
将是要使用的正则表达式。.gitignore
文件更改(请注意,不确定是否需要推送,如果完成也没有什么坏处)。2) 从 git 中删除文件(仅)。
git rm --cached some_dir/
从 git (仅)删除文件,3) 添加并提交该更改(本质上,这是对“添加”删除内容的更改,尽管“添加”命令令人困惑!)
git add .
git commit -m"removal"
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
.gitignore
to ignore them - in many cases such as yours, the parent directory, e.g.log/
will be the regex to use..gitignore
file change (not sure if push needed mind you, no harm if done).2) Remove the files from git (only).
git rm --cached some_dir/
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"
以上答案对我不起作用。 我使用
filter-branch
删除所有已提交的文件。使用以下命令从 git 存储库中删除文件: 使用
以下命令从 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:
Remove a folder from a git repository with:
This removes the directory or file from all the commits.
You can specify a commit by using:
Or an range:
To push everything to remote, you can do:
我想添加 @bdonlan 已接受的答案。
不要使用此答案来删除远程上存在的文件。
答案应该做什么?
它应该从本地暂存区域中删除您在之前的某些提交中错误提交的一些文件。
它将文件从已跟踪
I would like to add to the accepted answer of @bdonlan.
DON'T USE THIS ANSWER TO REMOVE FILE(S) THAT EXISTS ON REMOTE.
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).
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 ????????????????. )
我使用以下简单方法从 git 中删除了一些与 IDE 相关的文件,因为它们使存储库看起来很混乱。
注意:这不会将它们从 git 历史记录中删除。
注意:如果您不小心提交了密码,首先要做的就是更改这些密码。
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.
git status
and check that the files are not listed.这取决于您所说的从 git 中“删除”的含义。 :)
您可以使用 git rm --cached 查看更多详细信息。 当您取消暂存某些内容时,这意味着不再跟踪它,但这不会从以前的提交中删除该文件。
如果您想做的不仅仅是取消暂存文件,例如从所有以前的提交中删除敏感数据,您将需要考虑使用 BFG 仓库清理器。
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.
由于
rm --cache
会删除远程存储库中的文件,因此您可以使用update-index
代替请参阅: https://learn.microsoft.com/en-us/azure/devops/repos/git/ignore-files?view=azure-devops&tabs=visual-studio-2019
since
rm --cache
will delete files in the remote repository, you could useupdate-index
insteadsee: https://learn.microsoft.com/en-us/azure/devops/repos/git/ignore-files?view=azure-devops&tabs=visual-studio-2019
如果您想删除存储库中的文件而不是文件系统中的文件,则意味着您不想保留该文件的跟踪。 在 Git 中,您有 4 个替代方案(如此处指定)。
您可以使用 .gitignore 文件。 在这种情况下,文件/文件夹在本地未跟踪。
您可以按照与上一个案例类似的方式编辑.git/info/exclude。 这里的区别在于该文件在本地未跟踪。
您可以使用 git update-index Should-unchanged命令来跟踪文件,但本地更改将被忽略。
您可以使用 git update-index Skip-worktree命令来跟踪文件,但本地更改将被忽略。
第3点和第4点看似做同样的事情,但也有区别。 在此链接中,您可以找到有关这些点的行为的广泛测试列表。 总结一下差异,我们可以说标志
assume-unchanged
假设开发人员不再触及文件。 标志skip-worktree
指示 git 不再触及特定文件。 当您向存储库添加配置文件但不想跟踪其更改时,它非常有用。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).
You can use a .gitignore file. In this case the file/folder is locally untracked.
You can edit .git/info/exclude in a similar way as the previous case. Here the difference is that the file is locally untracked.
You can use the
git update-index assume-unchanged <filename>
command to track a file, but the local changes are ignored.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 flagskip-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.