如何从暂存区删除单个文件(撤消 git add)? [英] How do I remove a single file from the staging area (undo git add)?

查看:21
本文介绍了如何从暂存区删除单个文件(撤消 git add)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我有一个 Git 存储库,其中的文件已在索引中.我对几个文件进行了更改,打开 Git 并使用git add"将这些文件添加到我的暂存区.

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

解决方案

如果我正确理解了这个问题,您只是想撤消"为该文件完成的 git add.

如果您需要从暂存区删除单个文件,请使用

git reset HEAD --

如果您需要从暂存区删除整个目录(文件夹),请使用

git reset HEAD --

您的修改将被保留.当您运行 git status 时,该文件将再次显示为已修改但尚未暂存.

参见 git reset 手册页详情.

Situation: I have a Git repository with files already in the index. I make changes to several files, open Git and add these files to my staging area with "git add ."

Question: How do I remove one of those files from the staging area but not remove it from the index or undo the changes to the file itself?

解决方案

If I understand the question correctly, you simply want to "undo" the git add that was done for that file.

If you need to remove a single file from the staging area, use

git reset HEAD -- <file>

If you need to remove a whole directory (folder) from the staging area, use

git reset HEAD -- <directoryName>

Your modifications will be kept. When you run git status the file will once again show up as modified but not yet staged.

See the git reset man page for details.

这篇关于如何从暂存区删除单个文件(撤消 git add)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆