git - 如何在提交前撤消 'git add'?

标签 git undo git-add

<分区>

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

git add myfile.txt

我还没有运行 git commit。我如何撤消此操作,以便这些更改不会包含在提交中?

最佳答案

撤销 git add对于未提交的更改:

git reset <file>

这将从当前索引(“即将提交”列表)中删除文件而不更改任何其他内容。


取消暂存所有文件的所有更改:

git reset

在旧版本的 Git 中,以上命令等同于 git reset HEAD <file>git reset HEAD分别,如果 HEAD 将失败未定义(因为您尚未在存储库中进行任何提交)或不明确(因为您创建了一个名为 HEAD 的分支,这是您不应该做的愚蠢的事情)。这was changed in Git 1.8.2 , 不过,因此在现代版本的 Git 中,您甚至可以在首次提交之前使用上面的命令:

"git reset" (without options or parameters) used to error out when you do not have any commits in your history, but it now gives you an empty index (to match non-existent commit you are not even on).

文档:git reset

关于git - 如何在提交前撤消 'git add'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/348170/

相关文章:

git - 如何使用 GitHub API 获取任何分支中的一个文件的提交历史记录?

git - 如何在 Git 中将以前的版本恢复为新提交?

ios - 撤销按钮 Swift

git - 如何使用寻呼机进行长 git add --patch 大块头?

git - 回到一个特定的提交然后回到现在

git - vim diff 可以使用 patience 算法吗?

git - 如何用普通提交压缩 merge 提交?

drawing - Photoshop 撤销系统

git - 'git add --patch' 来包含新文件?

git - 如何通过 git status 中的编号在 git 中添加特定文件?