如何删除 Git 存储库中多个已删除的文件

发布于 2024-11-07 10:26:55 字数 1016 浏览 0 评论 0原文

我删除了一些文件,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 技术交流群。

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

发布评论

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

评论(16

听不够的曲调 2024-11-14 10:26:55
git add -u 

更新您的所有更改

git add -u 

updates all your changes

空‖城人不在 2024-11-14 10:26:55

对 git rm 非常谨慎。;它可能会删除比您想要的更多的内容。当然,您可以恢复,但不必这样做更简单。

最简单的是:

git rm modules/welcome/language/english/kaimonokago_lang.php \
       modules/welcome/language/french/kaimonokago_lang.php \
       modules/welcome/language/german/kaimonokago_lang.php \
       modules/welcome/language/norwegian/kaimonokago_lang.php

你不能使用 shell 通配符,因为文件不存在,但你可以使用(至少在 Bash 中):

git rm modules/welcome/language/{english,french,german,norwegian}/kaimonokago_lang.php

或者考虑:

git status | sed -n '/^# *deleted:/s///p' | xargs git rm

这需要 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:

git rm modules/welcome/language/english/kaimonokago_lang.php \
       modules/welcome/language/french/kaimonokago_lang.php \
       modules/welcome/language/german/kaimonokago_lang.php \
       modules/welcome/language/norwegian/kaimonokago_lang.php

You can't use shell wildcards because the files don't exist, but you could use (in Bash at least):

git rm modules/welcome/language/{english,french,german,norwegian}/kaimonokago_lang.php

Or consider:

git status | sed -n '/^# *deleted:/s///p' | xargs git rm

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 the deleted: and prints what is left; xargs gathers up the arguments and provides them to a git rm command. This works for any number of files regardless of similarity (or dissimilarity) in the names.

冷月断魂刀 2024-11-14 10:26:55

ByScripts 答案的另一个版本是

git rm $(git ls-files --deleted)

这只会从 git 中删除已删除的文件。

它也可用于仅添加修改过的文件。

git add $(git ls-files --modified)

这些命令也适用于 Windows 版 gitbash。

Another version to ByScripts answer is

git rm $(git ls-files --deleted)

This will ONLY remove the deleted files from the git.

It could be also be used for adding ONLY modified files also.

git add $(git ls-files --modified)

These commands also works on gitbash for windows.

心房的律动 2024-11-14 10:26:55

更新您所做的所有更改:

git add -u

已删除的文件应从未暂存(通常为红色)更改为已暂存(绿色)。
然后提交删除已删除的文件:

git commit -m "note"

Update all changes you made:

git add -u

The deleted files should change from unstaged (usually red color) to staged (green).
Then commit to remove the deleted files:

git commit -m "note"
草莓酥 2024-11-14 10:26:55

如果您不关心暂存修改后的文件,最好的解决方案是使用 mshameers 和/或 pb2q 所说的 git add -u

如果您只想删除已删除的文件,而不是暂存任何修改过的文件,我认为您应该将 ls-files 参数与 --deleted 选项一起使用(无需使用正则表达式或其他复杂的参数/选项):

git ls-files --deleted | xargs git rm

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 ls-files --deleted | xargs git rm
自此以后,行同陌路 2024-11-14 10:26:55

是的, git rm将暂存文件的已删除状态,其中 可以是 glob 模式:

$ git rm modules/welcome/language/*/kaimonokago_lang.php
rm modules/welcome/language/english/kaimonokago_lang.php
rm modules/welcome/language/french/kaimonokago_lang.php
rm modules/welcome/language/german/kaimonokago_lang.php
rm modules/welcome/language/norwegian/kaimonokago_lang.php

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    modules/welcome/language/english/kaimonokago_lang.php
#       ...

然后,您可以提交。

如果您愿意,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:

$ git rm modules/welcome/language/*/kaimonokago_lang.php
rm modules/welcome/language/english/kaimonokago_lang.php
rm modules/welcome/language/french/kaimonokago_lang.php
rm modules/welcome/language/german/kaimonokago_lang.php
rm modules/welcome/language/norwegian/kaimonokago_lang.php

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    modules/welcome/language/english/kaimonokago_lang.php
#       ...

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.

花辞树 2024-11-14 10:26:55

当我删除了很多未暂存以供提交的文件时,您可以使用 git rm 将它们全部集中显示:

for i in `git status | grep deleted | awk '{print $3}'`; do git rm $i; done

正如问题解答者提到的,请小心使用 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:

for i in `git status | grep deleted | awk '{print $3}'`; do git rm $i; done

As question answerer mentioned, be careful with git rm.

梦巷 2024-11-14 10:26:55

试试这个:

 git rm `git ls-files -d`

Try this:

 git rm `git ls-files -d`
寒尘 2024-11-14 10:26:55

您可以使用

git commit -m "remove files" -a
git push

手动删除文件后。

You can use

git commit -m "remove files" -a
git push

After you had delete files manually.

一枫情书 2024-11-14 10:26:55

您可以创建一个 shell 脚本,该脚本将在运行时删除所有文件:

git status | grep deleted | awk '{print "git rm " $3;}' > ../remove.sh

创建的脚本是 remove.sh,它包含 git rm 命令的完整列表。

You can create a shell script which will remove all your files when run:

git status | grep deleted | awk '{print "git rm " $3;}' > ../remove.sh

The script that is created is remove.sh and it contains the full list of git rm commands.

爱你是孤单的心事 2024-11-14 10:26:55

在我删除它们并遇到这个简洁的命令后,我的存储库中出现了幽灵文件的问题!

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 and git 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)

故事灯 2024-11-14 10:26:55

如果你想使用“git rm”删除所有这些。这就是我所做的:

git ls-files --deleted -z | xargs -0 git rm

此查询列出了所有已删除的文件,并将它们从 git 存储库中删除。
希望有帮助。

If you want to delete all of them by using "git rm". This is what I do:

git ls-files --deleted -z | xargs -0 git rm

This query lists of all the files that have been removed and deletes them from your git repository.
Hope it helps.

め七分饶幸 2024-11-14 10:26:55
git status | sed 's/^#\s*deleted:\s*//' | sed 's/^#.*//' | xargs git rm -rf
git status | sed 's/^#\s*deleted:\s*//' | sed 's/^#.*//' | xargs git rm -rf
蝶舞 2024-11-14 10:26:55

内置的清理功能也很有帮助......

git clean -fd

The built in clean function can also be helpful...

git clean -fd
岁月打碎记忆 2024-11-14 10:26:55
git add -u .

git add --update .
git add -u .

git add --update .
久光 2024-11-14 10:26:55

以下是如何检测已删除的文件并将其删除作为下一次提交的一部分。该线程上的所有解决方案都有不同的优点。下面的这个解决方案专门处理文件名中含有空格的问题。

git status --porcelain | awk '/^.D .*$/ {print $0}' | sed 's/.D \(.*\)/\1/' | tr -d '"' | xargs -I {} git rm '{}'

确保在使用以下命令运行之前使用 git 的 --dry-run 选项对其进行测试:

git status --porcelain | awk '/^.D .*$/ {print $0}' | sed 's/.D \(.*\)/\1/' | tr -d '"' | xargs -I {} git rm --dry-run '{}'

解释:

git status --porcelain

这会打印出类似的内容
D“/文件夹路径/文件路径”
仅当路径名中有空格时才会发生,

awk '/^.D .*$/ {print $0}'

仅匹配以“ D ”开头的行,

sed 's/ D \(.*\)/\1/'

从每个字符串前面删除“ D ”

tr -d '"'

,删除引号(如果有)将

xargs -I {} git rm '{}'

文件名变量定义为 {}
在 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.

git status --porcelain | awk '/^.D .*$/ {print $0}' | sed 's/.D \(.*\)/\1/' | tr -d '"' | xargs -I {} git rm '{}'

make sure you test this with git's --dry-run option before running it with the following:

git status --porcelain | awk '/^.D .*$/ {print $0}' | sed 's/.D \(.*\)/\1/' | tr -d '"' | xargs -I {} git rm --dry-run '{}'

explanation:

git status --porcelain

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

awk '/^.D .*$/ {print $0}'

match only lines that start with " D "

sed 's/ D \(.*\)/\1/'

remove " D " from the front of each string

tr -d '"'

remove quotes, if any

xargs -I {} git rm '{}'

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.

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