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

git add myfile.txt

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


当前回答

您可以使用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

其他回答

git rm --cached . -r

将“ un- add- add” 添加您从当前目录中添加的所有内容递归到当前目录

在源树中,您可以通过 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 rm --cached FILE

使用 rm -- -- 刻录仅用于新文件不小心添加 。

您可以使用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

为了澄清:git add将当前工作目录的更改移动到中转区(索引)。

此进程被称为此进程中 中 中。因此,最自然的命令阶段更改(已修改的文件)是显而易见的:

git stage

git add是一个简单到类型化的别名git stage

可惜没有git unstagegit unadd命令。相关命令更难猜或记,但很明显:

git reset HEAD --

我们可以很容易地为此创建别名:

git config --global alias.unadd 'reset HEAD --'
git config --global alias.unstage 'reset HEAD --'

最后,我们有了新的命令:

git add file1
git stage file2
git unadd file2
git unstage file1

我个人甚至使用更短的化名:

git a # For staging
git u # For unstaging