情况:我有一个Git存储库,索引中已经有文件。我修改了几个文件,打开Git,用“Git add”把这些文件添加到我的暂存区。

问:如何从暂存区删除其中一个文件,但不从索引中删除它,或撤消对文件本身的更改?


当前回答

如果你想删除遵循某种模式的文件,并且你正在使用git rm——cached,你也可以使用file-glob模式。

在这里看到的。

其他回答

我的示例:

$ git status
On branch feature/wildfire/VNL-425-update-wrong-translation
Your branch and 'origin/feature/wildfire/VNL-425-update-wrong-translation' have diverged,
and have 4 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   ShopBack/Source/Date+Extension.swift
    modified:   ShopBack/Source/InboxData.swift
    modified:   ShopBack/en.lproj/Localizable.strings

你们可能注意到了

> Changes to be committed:
>       (use "git reset HEAD <file>..." to unstage)

Git checkout——<文件>

它完美地从暂存区删除文件

Git重置<文件>

无论您是否有任何以前的提交。

对于新版本的Git,有Git restore——staging <file>。

当我用git 2.26.2.windows做git状态时。1 .也建议用于取消分期:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)

(这篇文章显示,在早期版本git重置HEAD在这一点上是推荐的)

我强烈推荐这篇文章,解释了git恢复、git恢复和git重置之间的区别,以及git恢复的附加参数。

git reset filename.txt

如果在filename.txt中有修改,则是您错误地将其添加到stage,并且希望从staging中删除该文件,但又不想丢失所做的更改。