0

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]$

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

5 Comments

What if I already added them using git add .? Maybe do git checkout -- *, and then do as you say?
git add . will only add files it physically finds in the working directory. So you will not be able to stage file removals that way. And using git add -u will not affect your already staged changes; it works additionally, so whatever you staged stays staged but in addition all changes in the “Changed but not updated” section are also added.
Didn't attempt to check them out, but just did git add -u, and all is good.
git checkout -- * will actually undo all unstaged changes in the current directory. So you should not do that if you want to keep your changes :)
@poke Except it doesn't undo deletions, because * is interpreted by the shell, and the shell doesn't know about the deleted files.
1

You're looking for git add -u (or --update). From man git add:

This removes as well as modifies index entries to match the working tree, but adds no new files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.