How to remove multiple deleted files in Git repository
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 add -u
updates all your changes
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.
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.
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.
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