我使用命令错误地将文件添加到 Git :

git add myfile.txt

我还没跑呢git commit。我如何撤销这些修改,使这些修改不包含在承诺中?


当前回答

请注意,如果您没有指定修改,则必须包含一个分隔符。例如,我的控制台:

git reset <path_to_file>
fatal: ambiguous argument '<path_to_file>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

git reset -- <path_to_file>
Unstaged changes after reset:
M    <path_to_file>

(1.7.5.4版总版)

其他回答

我会用git restore --staged .git restore --staged <filename>

您也可以使用git rm --cached然而,git rm命令应最好用于已跟踪的文件 。

如果你在初始承诺中,你不能使用git reset,只要声明“破产破产”并删除.git文件夹重新开始

使用*命令一次处理多个文件 :

git reset HEAD *.prj
git reset HEAD *.bmp
git reset HEAD *gdb*

假设我创造了一个新的文件,newFile.txt:

Enter image description here

假设我无意中添加文件git add newFile.txt:

Enter image description here

现在我想撤销此添加, 在承诺之前,git reset newFile.txt:

Enter image description here

git reset filename.txt

将删除命名的文件filename.txt从当前索引(也称为“存储区域”,即“即将实施”的改动”被保存的地方)中取出,不作任何其他改动(工作目录没有被覆盖)。