如何删除 Git 存储库中多个已删除的文件
我删除了一些文件,git status 显示如下。
我已经承诺并推动。
GitHub 仍显示存储库中已删除的文件。如何删除 GitHub 存储库中的文件?
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: modules/welcome/language/english/kaimonokago_lang.php
# deleted: modules/welcome/language/french/kaimonokago_lang.php
# deleted: modules/welcome/language/german/kaimonokago_lang.php
# deleted: modules/welcome/language/norwegian/kaimonokago_lang.php
如果我使用 git rm ,它会给出以下内容。
usage: git rm [options] [--] <file>...
-n, --dry-run dry run
-q, --quiet do not list removed files
--cached only remove from the index
-f, --force override the up-to-date check
-r allow recursive removal
--ignore-unmatch exit with a zero status even if nothing matched
I have deleted some files and git status shows as below.
I have committed and pushed.
GitHub still shows the deleted files in the repository. How can I delete files in the GitHub repository?
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: modules/welcome/language/english/kaimonokago_lang.php
# deleted: modules/welcome/language/french/kaimonokago_lang.php
# deleted: modules/welcome/language/german/kaimonokago_lang.php
# deleted: modules/welcome/language/norwegian/kaimonokago_lang.php
If I use git rm
, it gives the following.
usage: git rm [options] [--] <file>...
-n, --dry-run dry run
-q, --quiet do not list removed files
--cached only remove from the index
-f, --force override the up-to-date check
-r allow recursive removal
--ignore-unmatch exit with a zero status even if nothing matched
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
更新您的所有更改
updates all your changes
对 git rm 非常谨慎。;它可能会删除比您想要的更多的内容。当然,您可以恢复,但不必这样做更简单。
最简单的是:
你不能使用 shell 通配符,因为文件不存在,但你可以使用(至少在 Bash 中):
或者考虑:
这需要 git status 的输出,不默认情况下不会打印任何内容 (
sed -n
),但在以#deleted:
开头的行上,它会删除#
和 < code>deleted: 并打印剩下的内容; xargs 收集参数并将它们提供给 git rm 命令。这适用于任意数量的文件,无论名称是否相似(或不相似)。Be very cautious about
git rm .
; it might remove more than you want. Of course, you can recover, but it is simpler not to have to do so.Simplest would be:
You can't use shell wildcards because the files don't exist, but you could use (in Bash at least):
Or consider:
This takes the output of
git status
, doesn't print anything by default (sed -n
), but on lines that start# deleted:
, it gets rid of the#
and thedeleted:
and prints what is left;xargs
gathers up the arguments and provides them to agit rm
command. This works for any number of files regardless of similarity (or dissimilarity) in the names.ByScripts 答案的另一个版本是
这只会从 git 中删除已删除的文件。
它也可用于仅添加修改过的文件。
这些命令也适用于 Windows 版 gitbash。
Another version to ByScripts answer is
This will ONLY remove the deleted files from the git.
It could be also be used for adding ONLY modified files also.
These commands also works on gitbash for windows.
更新您所做的所有更改:
已删除的文件应从未暂存(通常为红色)更改为已暂存(绿色)。
然后提交删除已删除的文件:
Update all changes you made:
The deleted files should change from unstaged (usually red color) to staged (green).
Then commit to remove the deleted files:
如果您不关心暂存修改后的文件,最好的解决方案是使用 mshameers 和/或 pb2q 所说的
git add -u
。如果您只想删除已删除的文件,而不是暂存任何修改过的文件,我认为您应该将
ls-files
参数与--deleted
选项一起使用(无需使用正则表达式或其他复杂的参数/选项):The best solution if you don't care about staging modified files is to use
git add -u
as said by mshameers and/or pb2q.If you just want to remove deleted files, but not stage any modified ones, I think you should use the
ls-files
argument with the--deleted
option (no need to use regex or other complex args/options) :是的, git rm将暂存文件的已删除状态,其中
可以是 glob 模式:然后,您可以提交。
如果您愿意,
git commit -a
将一次性完成此操作。您还可以使用 git add -u 暂存所有更改(包括所有已删除的文件),然后提交。
Yes,
git rm <filename>
will stage the deleted state of a file, where<filename>
could be a glob pattern:Then, you can commit.
git commit -a
will do this in one go, if you want.You can also use
git add -u
to stage all the changes, including all the deleted files, then commit.当我删除了很多未暂存以供提交的文件时,您可以使用 git rm 将它们全部集中显示:
正如问题解答者提到的,请小心使用 git rm代码>.
When I have a lot of files I've deleted that are unstaged for commit, you can
git rm
them all in one show with:As question answerer mentioned, be careful with
git rm
.试试这个:
Try this:
您可以使用
手动删除文件后。
You can use
After you had delete files manually.
您可以创建一个 shell 脚本,该脚本将在运行时删除所有文件:
创建的脚本是
remove.sh
,它包含git rm
命令的完整列表。You can create a shell script which will remove all your files when run:
The script that is created is
remove.sh
and it contains the full list ofgit rm
commands.在我删除它们并遇到这个简洁的命令后,我的存储库中出现了幽灵文件的问题!
git add -A
本质上与 git add -a 和 git add -u 组合相同,但区分大小写。我从 这个很棒的链接(此链接现在指向 archive.org 上的版本,因为截至 2016 年 6 月,原始页面已转换为垃圾邮件/网络钓鱼页面)
I had this issue of ghost files appearing in my repo after I deleted them and came across this neat command!
git add -A
It's essentially the same as
git add -a
andgit add -u
combined, but it's case sensitive. I got it from this awesome link (this link points to the version on archive.org now, because the original has converted to a spam/phishing page as of June 2016)如果你想使用“git rm”删除所有这些。这就是我所做的:
此查询列出了所有已删除的文件,并将它们从 git 存储库中删除。
希望有帮助。
If you want to delete all of them by using "git rm". This is what I do:
This query lists of all the files that have been removed and deletes them from your git repository.
Hope it helps.
内置的清理功能也很有帮助......
The built in clean function can also be helpful...
以下是如何检测已删除的文件并将其删除作为下一次提交的一部分。该线程上的所有解决方案都有不同的优点。下面的这个解决方案专门处理文件名中含有空格的问题。
确保在使用以下命令运行之前使用 git 的 --dry-run 选项对其进行测试:
解释:
这会打印出类似的内容
D“/文件夹路径/文件路径”
仅当路径名中有空格时才会发生,
仅匹配以“ D ”开头的行,
从每个字符串前面删除“ D ”
,删除引号(如果有)将
文件名变量定义为 {}
在 git rm 下运行文件名,用单引号括起来,以确保它支持带空格的文件名。
Here is how to detect deleted files and stage their deletion as part of the next commit. All the solutions on this thread have different merits. This solution bellow specifically deals with the problem of file names with spaces in them.
make sure you test this with git's --dry-run option before running it with the following:
explanation:
This prints out something like
D "/path to a folder/path to a file"
which happens only when there are spaces in the path names
match only lines that start with " D "
remove " D " from the front of each string
remove quotes, if any
define file name variables as {}
run file name under git rm enclosed in single quotes in order to make sure that it supports file names with spaces.