How to remove multiple deleted files in Git reposi

2019-01-29 14:40发布

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

标签: git git-rm
16条回答
甜甜的少女心
2楼-- · 2019-01-29 15:01
git add -u 

updates all your changes

查看更多
祖国的老花朵
3楼-- · 2019-01-29 15:03

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.

查看更多
狗以群分
4楼-- · 2019-01-29 15:04

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.

查看更多
Rolldiameter
5楼-- · 2019-01-29 15:06

You can use

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

After you had delete files manually.

查看更多
淡お忘
6楼-- · 2019-01-29 15:07

Try this:

 git rm `git ls-files -d`
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-29 15:08

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

git clean -fd
查看更多
登录 后发表回答