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

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


当前回答

如果您只是想删除文件更改的子集,您可以使用:

git reset -p

or

git reset -p <file_name>

这个命令基本上与git add -p相反:它只会从暂存区域删除所选的更改。我发现它在“删除”我错误添加的东西时非常有用。

其他回答

我的示例:

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

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

Git重置<文件>

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

git rm --cached FILE

,

git rm -r --cached CVS */CVS