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

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


当前回答

我的示例:

$ 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,有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恢复的附加参数。

所以,对Tim Henigan的回答做一个小小的调整:你需要在文件名之前使用——。它看起来是这样的:

git reset HEAD -- <file>

你想要的:

影响到单个文件 从暂存区删除文件 不能从索引中删除单个文件 不要撤消更改本身

答案是

git reset HEAD file_name.ext

or

git reset HEAD path/to/file/file_name.ext

我的示例:

$ 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 reset filename.txt

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