假设我在git存储库中有一个名为foo的文件。

假设它已经被rm删除了(不是git rm)。然后git的状态会显示:

Changes not staged for commit:

    deleted: foo

我如何进行这个单独的文件删除?

如果我试着:

git add foo

它说:

'foo' did not match any files.

更新(9年后,哈哈):

看起来git 2.x已经修复了这个问题:

$ git --version
git version 2.25.1

$ mkdir repo

$ cd repo

$ git init .
Initialized empty Git repository in repo/.git/

$ touch foo bar baz

$ git add foo bar baz

$ git commit -m "initial commit"
[master (root-commit) 79c736b] initial commit
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 bar
create mode 100644 baz
create mode 100644 foo

$ rm foo

$ git status
On branch master
Changes not staged for commit:
    deleted: foo

$ git add foo

$ git status
On branch master
Changes to be committed:
    deleted:    foo

当前回答

要执行所有手动删除的文件,您可以使用:

git rm $(git ls-files --deleted)

为命令添加别名git rm-deleted,执行:

git config --global alias.rm-deleted '!git rm $(git ls-files --deleted)'

其他回答

你可以使用

Git rm -r——cached——"path/to/directory"

执行已删除的目录。

如果你想简单地将所有删除的文件添加到舞台上,那么你可以使用git add。

这是目前git v2.27.0中最简单的方法。注意使用*和。是不同的方法。使用git add *只会添加当前存在的文件,而git add。还会运行使用rm命令删除的文件。

很明显,但值得一提的是,当你使用git add时,其他被修改过的文件也会被添加到暂存区。

您可以使用该命令

git add `git ls-files --deleted`

解释:

git ls-files——deleted -该命令将返回所有已删除文件的文件名。

即使使用git rm [FILE]是正确的,你也可以使用git add -u。

根据git-add文档:

- u - - -更新 在索引已经有匹配[FILE]的条目的地方更新索引。这将删除和修改索引项以匹配 工作树,但不添加新文件。 如果在使用-u选项时没有给出[FILE],则整个工作树中所有被跟踪的文件都会被更新(使用旧版本的Git) 将更新限制到当前目录及其子目录)。

在此基础上,索引将被刷新,文件将被正确地暂存。

从Git 2.0.0开始,Git add也会执行文件删除。

Git Docs 2.0.0 - Go-add

< pathspec >… Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to update the index to match the current state of the directory as a whole (e.g. specifying dir will record not just a file dir/file1 modified in the working tree, a file dir/file2 added to the working tree, but also a file dir/file3 removed from the working tree. Note that older versions of Git used to ignore removed files; use --no-all option if you want to add modified or new files but ignore removed ones.