git - 暂存删除的文件

标签 git git-add git-rm git-stage

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

假设已经用rm(不是git rm)删除了。然后 git status 会显示:

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 foo 暂存文件以供删除。 (这也会从文件系统中删除该文件,如果它之前没有被删除的话。当然,它可以从 git 中恢复,因为它之前已经被 checkin 了。)

要暂存要删除的文件而不将其从文件系统中删除,请使用 git rm --cached foo

关于git - 暂存删除的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12373733/

相关文章:

php - 将应用程序部署到 heroku 时找不到 Procfile

git - TortoiseGit 日志在某些环境中未显示所有预期的提交消息

git - 使用 git-filter-branch 提取多个目录

git - 将 Git 分支直接推送到实时服务器目录中,以便实时查看文件

git - 对已 git 添加然后 git 提交的文件进行更改似乎工作得很好

git - 使用 git add --patch <filename> 手动编辑

Git 从所有提交中删除文件

Git:修改工作树和索引的 git checkout

git - 更改 .gitignore 时,所有开发人员都需要运行 `git rm --cached <path>` 吗?

git - 如何恢复 `git rm abc.c`之后的文件?