<分区>
我错误地使用以下命令将文件添加到 Git:
git add myfile.txt
我还没有运行 git commit
。我如何撤消此操作,以便这些更改不会包含在提交中?
<分区>
我错误地使用以下命令将文件添加到 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/