从远程 Git 存储库中删除物理删除文件的引用

发布于 2024-10-27 04:35:04 字数 159 浏览 2 评论 0原文

我已经从我的 Git 存储库(来自许多不同的目录)物理删除了很多文件。

我仍然可以看到他们的参考,如果我向 GitHub 提交,旧文件仍然存在。

我需要从远程存储库服务器 (GitHub) 以及我的本地 PC 中删除这些引用。

如何从存储库中删除这些引用?

I have deleted a lot of files from my Git repository (from many different directories) physically.

I can still see their reference and if I make a commit to GitHub, old files are still there.

I need to remove those references from the remote repo server (GitHub) and as well as from my local PC.

How can I remove these references from the repositories?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说谎友 2024-11-03 04:35:04

您需要创建一个“提交”(git 的新版本术语)来删除这些文件,然后将其推送到 GitHub。我想如果你输入 git status ,你会看到类似这样的内容:

# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    old-file.txt
#   deleted:    another-old-file.txt
#   deleted:    unneeded.c

If there is no otherchanges returned in the output of git status, it would be safe to do :

git add -u
git commit -m "Delete many old files"

...然后推送到 GitHub。 (git add -u 表示为下一次提交暂存对 git 跟踪的文件的任何更改,其中包括您在本地手动删除的这些文件。)

但是,如果有该输出中列出了其他更改,最好尝试更精确的内容,例如此处建议的:

以后最好使用 git rm 删除文件 首先:)

You need to create a "commit" (git's terminology for a new version) which has those files deleted, and then push that to GitHub. I guess if you type git status, you'll see something like this:

# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    old-file.txt
#   deleted:    another-old-file.txt
#   deleted:    unneeded.c

If there are no other changes listed in the output of git status, it would be safe to do:

git add -u
git commit -m "Delete many old files"

... and then push to GitHub. (The git add -u says to stage for the next commit any changes to files that are being tracked by git, which would include these files which you've deleted by hand locally.)

However, if there are other changes listed in that output, it would be better to try something more precise, such as suggested here:

In future, it's best to delete files with git rm in the first place :)

糖果控 2024-11-03 04:35:04

如果您想从存储库的所有提交中删除文件,您需要 git filter-branch 。使用它的示例可以在整个互联网上找到,例如 GitHub

If you want to remove a file from all commits of the repository, you need git filter-branch. Examples for using it can be found all over the internet, e.g. on GitHub.

坐在坟头思考人生 2024-11-03 04:35:04

如果您正在谈论删除文件并提交此删除,那么您应该使用

git rm filename

此命令将删除文件本身并立即暂存删除以供提交。因此,在这种情况下,您只需要执行

git commit -m "Remove files" #everything staged will be committed

BTW,当您想要移动或重命名文件时也是如此。不要使用 shell move 命令来执行此操作,而是使用“git mv [source] [destination]”

现在,如果您在没有使用“git rm”的情况下删除了它们,

则应该首先暂存它们:

git add -u
git commit -m "Remove files"

现在,如果您希望它们从整个历史记录,因此不会提及这些文件,那么您必须使用

git filter-branch 

Git 过滤器-branch 文档

请注意,大多数时候您实际上不需要使用 filter-branch 命令,因为它是非常高级的东西。最好的示例用例是,如果您已提交包含一些关键信息(例如您的信用卡号和到期日期)的文件,并将该文件推送到公共存储库。在这种情况下,将此值更改为一些不相关的值或删除文件是不够的 - 您必须使用过滤器分支;)
...并祈祷没有人能够足够快地从您的存储库中获取;)
...然后报告您的信用卡被盗...;)

if you're talking about removing files and committing this removal then you should use

git rm filename

This command will remove the file itself and stage the removal for commit right away. So, in this case you will need only to execute

git commit -m "Remove files" #everything staged will be committed

BTW same is true when you wan to move or rename files. Don't do that using shell move command, but rather using "git mv [source] [destination]"

Now if you have removed them without using "git rm"

you should stage them first:

git add -u
git commit -m "Remove files"

Now if you want them to disappear from the entire history, so there will be no mentioning of those files whatsoever, then you have to use

git filter-branch 

Git filter-branch documentation

Please, note that most of the time you actually don't need to use filter-branch command as it is pretty advanced stuff. The best sample use case for it is if you have committed the file with some critical info like your credit card number and expiration date and pushed this file to public repo. In this case, changing this value to some irrelevant value or deleting file is not enough - you have to use filter branch;)
...And pray that nobody was quick enough to fetch from your repo;)
...And then report your credit card as stolen anyway...;)

早茶月光 2024-11-03 04:35:04

它们应该在您的索引中显示为已删除。只要暂存它们,它们就会被删除。

使用 git add -i 可以加快暂存过程。

They should show up in your index as deleted. Just stage them and they will be deleted.

Use git add -i to make the staging process quicker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文