如何从 Git 存储库中删除文件?
如何从我的存储库中删除“file1.txt”
?
How can I delete "file1.txt"
from my repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何从我的存储库中删除“file1.txt”
?
How can I delete "file1.txt"
from my repository?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(26)
使用
git rm
。如果您想从 Git 存储库和文件系统中删除文件,请使用:
但如果您只想从 Git 存储库中删除文件而不从文件系统中删除它,请使用:
并推送对远程仓库的更改
Use
git rm
.If you want to remove the file from the Git repository and the filesystem, use:
But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:
And to push changes to remote repo
git rm file.txt
会从存储库中删除该文件,但也会从本地文件系统中删除该文件。要从存储库中删除文件并且不从本地文件系统中删除它,请使用:
git rm --cached file.txt
下面的具体情况是我使用 git 维护公司网站的版本控制,但“mickey”目录是一个 tmp 文件夹,用于与 CAD 共享私有内容开发商。当他需要巨大的文件时,我创建了一个私有的、未链接的目录,并将文件上传到那里,以便他通过浏览器获取。我忘记了我这样做了,后来我从网站的基本目录中执行了 git add -A 。随后,
git status
显示需要提交的新文件。现在我需要从 git 的跟踪和版本控制中删除它们...下面的示例输出来自我刚刚发生的事情,我无意中删除了
.003
文件。值得庆幸的是,我不在乎.003
的本地副本发生了什么,但当前更改的其他一些文件是我刚刚对网站进行的更新,如果在本地文件系统! “本地文件系统”=实时网站(不是一个很好的做法,但确实是现实)。更新:这个答案获得了一些流量,所以我想我应该提到我的其他 Git 答案分享一些很棒的资源:此页面有一个图形可以帮助揭开神秘面纱给我的。 “Pro Git”一书已上线,对我帮助很大。
git rm file.txt
removes the file from the repo but also deletes it from the local file system.To remove the file from the repo and not delete it from the local file system use:
git rm --cached file.txt
The below exact situation is where I use git to maintain version control for my business's website, but the "mickey" directory was a tmp folder to share private content with a CAD developer. When he needed HUGE files, I made a private, unlinked directory and ftpd the files there for him to fetch via browser. Forgetting I did this, I later performed a
git add -A
from the website's base directory. Subsequently,git status
showed the new files needing committing. Now I needed to delete them from git's tracking and version control...Sample output below is from what just happened to me, where I unintentionally deleted the
.003
file. Thankfully, I don't care what happened to the local copy to.003
, but some of the other currently changed files were updates I just made to the website and would be epic to have been deleted on the local file system! "Local file system" = the live website (not a great practice, but is reality).Update: This answer is getting some traffic, so I thought I'd mention my other Git answer shares a couple of great resources: This page has a graphic that help demystify Git for me. The "Pro Git" book is online and helps me a lot.
首先,如果您使用
git rm
,特别是对于多个文件,请考虑任何通配符将由 shell 解析,而不是由 git 命令解析。但是,如果您的文件已在 GitHub 上,您可以(自 2013 年 7 月起)直接从Web GUI中删除它!
然后在本地存储库上“
git pull
”,这也会在本地删除该文件。这使得这个答案成为从 git repo 删除文件的(迂回)方式?
(更不用说 GitHub 上的文件位于“git 存储库”中)
< img src="https://i.sstatic.net/259au.jpg" alt="删除按钮">
(提交将反映该文件的删除):
最后一句意味着删除的文件仍然是历史记录的一部分,您可以轻松恢复它(但还不能通过 GitHub Web 界面):
请参阅“在 Git 存储库中恢复已删除的文件"。
First, if you are using
git rm
, especially for multiple files, consider any wildcard will be resolved by the shell, not by thegit
command.But, if your file is already on GitHub, you can (since July 2013) directly delete it from the web GUI!
Then "
git pull
" on your local repo, and that will delete the file locally too.Which makes this answer a (roundabout) way to delete a file from git repo?
(Not to mention that a file on GitHub is in a "git repo")
(the commit will reflect the deletion of that file):
The last sentence means that the deleted file is still part of the history, and you can restore it easily enough (but not yet through the GitHub web interface):
See "Restore a deleted file in a Git repo".
这是唯一对我有用的选择。
注意:将 *.sql 替换为您的文件名或文件类型。请务必小心,因为这将遍历每次提交并删除此文件类型。
编辑:
注意 - 执行此命令后,您将无法推送或拉取 - 您将看到“不相关的历史记录”被拒绝,您可以使用“git push --force -u origin master”来推送或拉取
This is the only option that worked for me.
Note: Replace *.sql with your file name or file type. Be very careful because this will go through every commit and rip this file type out.
EDIT:
pay attention - after this command you will not be able to push or pull - you will see the reject of 'unrelated history' you can use 'git push --force -u origin master' to push or pull
此外,如果它是要删除的文件夹及其后续子文件夹或文件,请使用:
Additionally, if it's a folder to be removed and it's subsequent child folders or files, use:
更一般地说,
git help
至少可以帮助解决如下简单问题:More generally,
git help
will help with at least simple questions like this:Greg Hewgill 的答案,由 Johannchopin 帮助了我,因为我不关心将该文件从历史记录中完全删除。
就我而言,它是一个目录,因此我所做的唯一更改是使用:
而不是“git rm --cached file1.txt”
..其次:
感谢Greg Hewgill和约翰肖平!
The answer by Greg Hewgill, that was edited by Johannchopin helped me, as I did not care about removing the file from the history completely.
In my case, it was a directory, so the only change I did was using:
instead of "git rm --cached file1.txt"
..followed by:
Thanks Greg Hewgill and Johannchopin!
如果您想从存储库中删除文件,但将其保留在文件系统中(将不被跟踪):
如果您想从存储库和文件系统中删除文件,则有两个选项:
如果该文件在索引中没有暂存的更改:
如果文件在索引中暂存更改:
If you want to delete the file from the repo, but leave it in the the file system (will be untracked):
If you want to delete the file from the repo and from the file system then there are two options:
If the file has no changes staged in the index:
If the file has changes staged in the index:
从现在开始, git rm 只会删除该分支上的文件,但它仍然保留在历史记录中,并且 git 会记住它。
正确的方法是使用 git filter-branch ,正如其他人在这里提到的那样。它将重写分支历史记录中的每个提交以删除该文件。
但是,即使这样做之后,git 仍然可以记住它,因为可以在 reflog、远程、标签等中引用它。
如果你想一步彻底抹掉它,我推荐你使用
gitforget-blob
https://ownyourbits.com/2017/01/18/completely-remove-a-file-from -a-git-repository-with-git-forget-blob/
这很简单,只需执行
gitforget-blob file1.txt
即可。这将删除所有引用,执行 git filter-branch,最后运行 git 垃圾收集器 git gc 以完全删除存储库中的此文件。
git rm
will only remove the file on this branch from now on, but it remains in history and git will remember it.The right way to do it is with
git filter-branch
, as others have mentioned here. It will rewrite every commit in the history of the branch to delete that file.But, even after doing that, git can remember it because there can be references to it in reflog, remotes, tags and such.
If you want to completely obliterate it in one step, I recommend you to use
git forget-blob
https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/
It is easy, just do
git forget-blob file1.txt
.This will remove every reference, do
git filter-branch
, and finally run the git garbage collectorgit gc
to completely get rid of this file in your repo.根据文档。
git rm --cached file1.txt
当涉及敏感数据时,最好不要说您删除了该文件,而只是将其包含在最后一次已知的提交中:
0。修改最后一次提交
如果要从所有 git 历史记录中删除该文件,请根据 文档 您应该执行以下操作:
1.将其从本地历史记录中删除
3. 如果您需要从远程删除
如果您还需要从标签版本中删除它:
According to the documentation.
git rm --cached file1.txt
When it comes to sensitive data—better not say that you removed the file but rather just include it in the last known commit:
0. Amend last commit
If you want to delete the file from all git history, according to the documentation you should do the following:
1. Remove it from your local history
3. If you need to remove from the remote
4. If you also need to remove it from tag releases:
如果您想使用 rm 命令从本地文件夹中删除文件,然后将更改推送到远程服务器,则还有另一种方法。
Another way if you want to delete the file from your local folder using rm command and then push the changes to the remote server.
注意:如果您只想从 git 中删除文件,请使用以下命令:
如果您还想从硬盘中删除:
如果您想删除一个文件夹(该文件夹可能包含几个文件),那么您应该使用递归命令删除,如下所示:
如果您想删除另一个文件夹中的文件夹
那么,您可以照常
commit
和push
。但是,如果您想恢复已删除的文件夹,可以按照以下步骤操作:恢复已删除的文件从 git 是可能的。阅读官方文档了解更多信息。
Note: if you want to delete file only from git use below:
If you want to delete also from hard disk:
If you want to remove a folder(the folder may contain few files) so, you should remove using recursive command, as below:
If you want to remove a folder inside another folder
Then, you can
commit
andpush
as usual. However, if you want to recover deleted folder, you can follow this: recover deleted files from git is possible.Read more on official doc.
https://stackoverflow.com/a/2047477/14508423
https://stackoverflow.com/a/19666677/14508423
很相似,两个都试过了。没有什么。 Git 不断跟踪该文件。 (我使用 vscode,因此文件立即被标记为 U(未跟踪)或 A(已添加))
这样做 https://stackoverflow.com /a/53431148/14508423 到整个类别解决了问题。
执行此操作可以从 git 中删除文件并使 git 忘记它:
ps:每次失败时,我都会尝试删除带有令牌(.env 和 settings.py 文件)等敏感信息的文件。但它可以很好地处理随机文件 oO
https://stackoverflow.com/a/2047477/14508423
https://stackoverflow.com/a/19666677/14508423
Quite similar, tried both. Nothing. Git keeps tracking the file. (I use vscode so the file got instantly marked as U(untracked) or A(added))
Doing this https://stackoverflow.com/a/53431148/14508423 to whole category solves the problem.
Do this to delete a file from git and make git forget about it:
ps: every time it failed I tried to delete file with sensitive info like tokens (.env and settings.py files). But it works fine with random files oO
就我而言,我尝试在几次提交后删除 github 上的文件,但保存在计算机上
,后来该文件被忽略
In my case I tried to remove file on github after few commits but save on computer
and later this file was ignored
删除特定文件
单次递归地清除目录中所有未跟踪的文件
To delete a specific file
To clean all the untracked files from a directory recursively in single shot
如果您有适用于 Windows 的 GitHub 应用程序,则只需 5 个简单步骤即可删除文件:
If you have the GitHub for Windows application, you can delete a file in 5 easy steps:
首先,从本地存储库中删除文件。
<块引用>
git rm -r 文件名
或者,仅从本地存储库中删除文件,但从文件系统中删除文件
<块引用>
git rm --缓存文件名
其次,将更改提交到本地存储库。
最后,将本地更改更新/推送到远程存储库。
<前><代码>git推送
First,Remove files from local repository.
or, remove files only from local repository but from filesystem
Secondly, Commit changes into local repository.
Finally, update/push local changes into remote repository.
对于
git rm
不够并且需要从历史记录中删除文件的情况:正如git filter-branch
手册页现在本身建议使用 git-filter-repo,我今天必须这样做,这是一个使用该工具的示例。它使用示例存储库https://example/eguser/eg.git
将存储库克隆到新目录
git clone https://example/eguser/eg.git< /代码>
保留除不需要的文件之外的所有内容。 git-filter-repo --path file1.txt --invert-paths
添加远程存储库源:
git 远程添加源 https://example/eguser/eg.git
。git-filter-repo
工具按设计删除远程远程信息,并建议 一个新的远程存储库(参见第 4 点)。这对于大型共享存储库来说是有意义的,但对于删除单个新添加的文件(如此例中所示)可能有点过分了。当对本地的内容感到满意时,用它替换远程。
git push --force -u origin master
。由于历史记录已更改,因此需要强制。另请注意有用的
--dry-run
选项以及链接手册中关于团队和项目动态的良好讨论,然后再充电和更改存储库历史记录。For the case where
git rm
doesn't suffice and the file needs to be removed from history: As thegit filter-branch
manual page now itself suggests using git-filter-repo, and I had to do this today, here's an example using that tool. It uses the example repohttps://example/eguser/eg.git
Clone the repository into a new directory
git clone https://example/eguser/eg.git
Keep everything except the unwanted file.
git-filter-repo --path file1.txt --invert-paths
Add the remote repository origin back :
git remote add origin https://example/eguser/eg.git
.The
git-filter-repo
tool removes remote remote info by design and suggests a new remote repo (see point 4). This makes sense for big shared repos but might be overkill for getting rid a single newly added file as in this example.When happy with the contents of local, replace remote with it.
git push --force -u origin master
. Forcing is required due to the changed history.Also note the useful
--dry-run
option and a good discussion in the linked manual on team and project dynamics before charging in and changing repository history.转到您的项目目录并输入:
之后 push --force 从所有提交中删除文件。
go to your project dir and type:
after that push --force for delete file from all commits.
2022 年有效的新答案。
请勿使用:
此命令在推送后可能不会更改远程存储库。如果您在使用后进行克隆,您会发现没有任何变化,并且存储库仍然很大。这个命令现在已经很旧了。例如,如果您使用 https://github.com/18F/C2/issues 中的步骤/439,这行不通。
您需要使用
步骤:
(1)查找.git中最大的文件:
(2)Strat过滤这些大文件:
或
或
或
无论您在步骤 1 中找到什么。
(3)
(4)
完成!
New answer that works in 2022.
Do not use:
this command might not change the remote repo after pushing. If you clone after using it, you will see that nothing has changed and the repo still has a large size. This command is old now. For example, if you use the steps in https://github.com/18F/C2/issues/439, this won't work.
You need to use
Steps:
(1) Find the largest files in .git:
(2) Strat filtering these large files:
or
or
or
whatever you find in step 1.
(3)
(4)
DONE!
我尝试了很多建议的选项,但似乎都不起作用(我不会列出各种问题)。
我最终所做的,有效的,简单直观(对我来说)是:
I tried a lot of the suggested options and none appeared to work (I won't list the various problems).
What I ended up doing, which worked, was simple and intuitive (to me) was:
使用 git rm 从存储库中删除文件后,您可以使用 BFG Repo-Cleaner 可以轻松地从存储库历史记录中彻底删除该文件。
After you have removed the file from the repo with
git rm
you can use BFG Repo-Cleaner to completely and easily obliterate the file from the repo history.只需转到 github 存储库中的文件,您就可以看到 Raw|Blame 旁边的删除图标,并且不要忘记单击提交更改按钮。您可以看到您的文件已被删除。
Just by going on the file in your github repository you can see the delete icon beside Raw|Blame and don't forget to click on commit changes button. And you can see that your file has been deleted.
我有 obj 和 bin 文件意外地进入了存储库,我不想污染我的“已更改文件”列表
之后我注意到它们去了远程,我通过将其添加到 < code>.gitignore
问题是它们已经在远程中,当它们发生更改时,它们会弹出为已更改并污染已更改的文件列表。
要停止查看它们,您需要从远程存储库中删除整个文件夹。
在命令提示符下:
C:\repos\MyRepo
)C:\repos\MyRepo\SSIS
)git rm -r -f 对象
git commit -m "remove objfolder"
我收到一条令人震惊的消息说13个文件更改了315222个删除
然后因为我不想查找CMD行,所以我进入Visual Sstudio并进行同步以将其应用到远程
I have obj and bin files that accidentally made it into the repo that I don't want polluting my 'changed files' list
After I noticed they went to the remote, I ignored them by adding this to
.gitignore
Problem is they are already in the remote, and when they get changed, they pop up as changed and pollute the changed file list.
To stop seeing them, you need to delete the whole folder from the remote repository.
In a command prompt:
C:\repos\MyRepo
)C:\repos\MyRepo\SSIS
)git rm -r -f obj
git commit -m "remove obj folder"
I got an alarming message saying 13 files changed 315222 deletions
Then because I didn't want to have to look up the CMD line, I went into Visual Sstudio and did a Sync to apply it to the remote
如果您需要从确定的扩展名中删除文件(例如,已编译的文件),您可以执行以下操作来一次性删除它们:
If you need to remove files from a determined extension (for example, compiled files) you could do the following to remove them all at once:
如果您不在本地存储库中归档,而是在 git 存储库中归档,则只需通过 Web 界面打开 git 存储库中的文件,然后在界面右上角找到“删除”按钮即可。
单击此处,查看界面删除选项
Incase if you don't file in your local repo but in git repo, then simply open file in git repo through web interface and find Delete button at right corner in interface.
Click Here, To view interface Delete Option