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

git add myfile.txt

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


当前回答

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

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

其他回答

在源树中,您可以通过 GUI 轻松地做到这一点。您可以检查源树命令用什么来取消文件 。

我创建了一个新的文件, 并将其添加到 Git 中。 然后我用“ 原始树 GUI” 解除了它。 这是结果 :

解除文件 [08/12/ 15 10: 43] git - c diff. mnemonicprefix= false - c c c核心. cootepath= false - creedical. helper= 源树重新设置 -q - 路径/ 到/ 文件/ files/ filename.java

源树用途reset以卸载新文件。

只是类型git reset它将复返,犹如你从未打过字一样。git add .自从你最后一次承诺之后,你一定要保证你以前的承诺。

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

Enter image description here

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

Enter image description here

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

Enter image description here

如果您想要还原最后一个任务, 但仍要保留在任务中本地作出的更改, 请使用此命令 :

git reset HEAD~1 --mixed

您可以使用g基基边命令或图形用户界面 Git 。

单个文件

git reset File.txt

多个文件

git reset File1.txt File2.txt File3.txt

示例示例示例示例

假设你添加了Home.js 居家公居, 项目表.js, 最新情况.js犯错,

Enter image description here

并要撤消/重置

git reset src/components/home/Home.js src/components/listItem/ListItem.js src/components/update/Update.js

Enter image description here

同一例子使用Git 图形界面

git gui

打开窗口。不检查您的文件分阶段变化(将承诺)

Enter image description here