是否可以在 git 中隐藏从索引中删除的文件?

Is it possible in git to stash removing a file from the index?

我想做:

git rm --cached file.autogenerated.txt
git stash -m "untrack file.autogenerated.txt"

存储已创建,但存储中似乎没有任何内容 - 只有 0 个文件更改、0 个插入、0 个删除。并且应用这个存储没有效果。

我也试过:

git rm --cached file.autogenerated.txt
git stash --include-untracked -m "untrack file.autogenerated.txt"

但是同样没有效果。

有什么方法可以隐藏从索引中删除的文件吗?

您可以re-apply删除使用git stash pop --index,例如

$ git add foo bar
$ git commit -m 'initial'
[master (root-commit) 4bf33f4] initial
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar
 create mode 100644 foo
$ git rm --cached bar
rm 'bar'
$ git stash
Saved working directory and index state WIP on master: 4bf33f4 initial
$ git status
On branch master
nothing to commit, working tree clean
$ git stash pop --index
Already up to date.
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    bar

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    bar

Dropped refs/stash@{0} (a998ca45e1230d40ac84403c9d84b5328e89b5bf)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    deleted:    bar

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    bar