简体   繁体   English

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

[英]How to remove multiple deleted files in Git repository

I have deleted some files and git status shows as below. 我删除了一些文件和git状态显示如下。

I have committed and pushed. 我承诺并推动。

GitHub still shows the deleted files in the repository. GitHub仍然在存储库中显示已删除的文件。 How can I delete files in the GitHub repository? 如何删除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

If I use git rm , it gives the following. 如果我使用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
git add -u 

更新所有更改

Be very cautious about git rm . git rm .非常谨慎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): 您不能使用shell通配符,因为文件不存在,但您可以使用(至少在Bash中):

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; 这取得了git status的输出,默认情况下不会打印任何内容( sed -n ),但是在启动# deleted: ,它会deleted: #deleted:并打印剩下的内容; xargs gathers up the arguments and provides them to a git rm command. xargs收集参数并将它们提供给git rm命令。 This works for any number of files regardless of similarity (or dissimilarity) in the names. 无论名称中的相似性(或不相似性)如何,这都适用于任意数量的文件。

Another version to ByScripts answer is ByScripts答案的另一个版本是

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

This will ONLY remove the deleted files from the git. 这只会从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. 这些命令也适用于Windows的gitbash。

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"

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 . 如果你不关心升级文件的最佳解决方案是使用mshameers和/或pb2q所说的git add -u

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) : 如果你只想删除已删除的文件,但不要删除任何已修改的文件,我认为你应该使用带有--deleted选项的ls-files参数(不需要使用正则表达式或其他复杂的args /选项):

git ls-files --deleted | xargs git rm

Yes, git rm <filename> will stage the deleted state of a file, where <filename> could be a glob pattern: 是的, git rm <filename>git rm <filename>的已删除状态,其中<filename>可以是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
#       ...

Then, you can commit. 然后,你可以提交。

git commit -a will do this in one go, if you want. 如果你愿意, git commit -a可以一次完成。

You can also use git add -u to stage all the changes, including all the deleted files, then commit. 您还可以使用git add -ugit add -u所有更改,包括所有已删除的文件,然后提交。

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: 当我有很多文件,我已经删除了这是不分阶段的承诺,你可以git rm他们都在一个放映:

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

As question answerer mentioned, be careful with git rm . 正如问题回答者提到的那样,小心git rm

试试这个:

 git rm `git ls-files -d`

You can use 您可以使用

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

After you had delete files manually. 手动删除文件后。

You can create a shell script which will remove all your files when run: 您可以创建一个shell脚本,该脚本将在运行时删除所有文件:

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. 创建的脚本是remove.sh ,它包含git rm命令的完整列表。

git status | sed 's/^#\s*deleted:\s*//' | sed 's/^#.*//' | xargs git rm -rf

If you want to delete all of them by using "git rm". 如果要使用“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. 此查询列出了已删除的所有文件,并从git存储库中删除它们。 Hope it helps. 希望能帮助到你。

git add -u .

git add --update .

内置的清洁功能也很有用......

git clean -fd

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. 它与git add -agit add -u基本相同,但它区分大小写。 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) 我从这个很棒的链接获得它(此链接指向archive.org上的版本,因为原始版本已转换为垃圾邮件/网上诱骗页面,截至2016年6月)

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的--dry-run选项测试之前使用以下命令运行它:

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 这会打印出类似D“/文件夹/路径到路径的路径”,只有在路径名中有空格时才会发生

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

match only lines that start with " D " 仅匹配以“D”开头的行

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

remove " D " from the front of each string 从每个字符串的前面删除“D”

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. 将文件名变量定义为{}在单引号括起的git rm下运行文件名,以确保它支持带空格的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM