user1032531 user1032531 - 29 days ago 9
Git Question

Deleting multiple files from git

Other than individually deleting each file (i.e.

git rm src/classes/Config.php
,
git rm src/public/ajax.php
, etc, etc), how can all these files be deleted from git?

[Michael@devserver main]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: src/classes/Config.php
# deleted: src/public/ajax.php
# deleted: src/public/fileuploader.php
# deleted: src/public/index2.php
# deleted: src/public/json_encode.php
# deleted: src/public/petstore.json
# deleted: src/public/resources/index.php
# deleted: src/public/slimttest.php
# deleted: src/public/temp.php
# deleted: src/public/test.php
# deleted: src/public/test/bla.php
# deleted: src/public/test/file1.php
# deleted: src/public/test/file2.php
# deleted: src/public/test2.php
# deleted: src/public/testAPI.php
# deleted: src/public/testAPI2.php
# deleted: src/public/test_original.php
# deleted: src/public/testfile.php
# deleted: temporary.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[Michael@devserver main]$

Answer

You can use git add --update or git add -u in short to stage all changes that are displayed in the “Changed but not updated” section in git status.

Using this option on git add will not stage untracked files, so it’s safe to use even when you have lots of other changes (unlike git add -A).

This is especially helpful for situations like yours where you have lots of removed files. If you have some changes you do not want to stage, you can afterwards use git reset <path> to unstage the change (without undoing that change of course).

Comments