如何删除 Git 存储库中的多个已删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6004453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove multiple deleted files in Git repository
提问by shin
I have deleted some files and git status shows as below.
我删除了一些文件,git status 显示如下。
I have committed and pushed.
我已经承诺并推动了。
GitHub still shows the deleted files in the repository. How can I delete files in the GitHub repository?
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
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
回答by mshameers
git add -u
updates all your changes
更新您的所有更改
回答by Jonathan Leffler
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.
非常谨慎git rm .
;它可能会删除比您想要的更多。当然,您可以恢复,但不必这样做更简单。
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; 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.
这需要git status
, 默认情况下不打印任何内容 ( sed -n
),但在 start 的行上# deleted:
,它去掉了#
和deleted:
并打印了剩下的内容;xargs
收集参数并将它们提供给git rm
命令。无论名称是否相似(或不同),这都适用于任意数量的文件。
回答by Vijay C
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。
回答by Aziz Alto
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"
回答by ByScripts
The best solution if you don't care about staging modified files is to use git add -u
as said by mshameersand/or pb2q.
如果你不关心分期修改文件的最佳解决方案是使用git add -u
如说mshameers和/或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) :
如果您只想删除已删除的文件,但不想暂存任何修改过的文件,我认为您应该使用ls-files
带有--deleted
选项的参数(无需使用正则表达式或其他复杂的参数/选项):
git ls-files --deleted | xargs git rm
回答by Ben James
Yes, git rm <filename>
will stage the deleted state of a file, where <filename>
could be a glob pattern:
是的,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 -u
暂存所有更改,包括所有已删除的文件,然后提交。
回答by Justin Mandzik
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 }'`; do git rm $i; done
As question answerer mentioned, be careful with git rm
.
正如问答者所提到的,小心使用git rm
.
回答by eludom
Try this:
尝试这个:
git rm `git ls-files -d`
回答by La-comadreja
You can create a shell script which will remove all your files when run:
您可以创建一个 shell 脚本,该脚本将在运行时删除所有文件:
git status | grep deleted | awk '{print "git rm " ;}' > ../remove.sh
The script that is created is remove.sh
and it contains the full list of git rm
commands.
创建的脚本是remove.sh
并且它包含git rm
命令的完整列表。
回答by onalbi
You can use
您可以使用
git commit -m "remove files" -a
git push
After you had delete files manually.
手动删除文件后。