如何在提交前撤销'git add'? [英] How to undo 'git add' before commit?

查看:98
本文介绍了如何在提交前撤销'git add'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I mistakenly added files to git using the command:

git add myfile.txt

我还没有运行 git commit 。有没有办法撤消这个,所以这些文件不会被包含在提交?

I have not yet run git commit. Is there a way to undo this, so these files won't be included in the commit?

推荐答案

您可以撤消 git add 在提交之前

You can undo git add before commit with

git reset <file>

这会将其从当前索引(即将提交列表)中删除而不更改任何内容

which will remove it from the current index (the "about to be committed" list) without changing anything else.

您可以使用

You can use

git reset

没有任何文件名称可以取消所有更改。当在一段合理的时间内逐一列出太多文件时,这可以派上用场。

without any file name to unstage all due changes. This can come in handy when there are too many files to be listed one by one in a reasonable amount of time.

在旧版本的Git中,上述命令是等效的到 git reset HEAD< file> git reset HEAD ,如果 HEAD 是未定义的(因为您尚未在您的仓库中进行任何提交)或含糊不清(因为您创建了一个名为 HEAD 的分支,一个你不应该做的愚蠢的事情)。这个已在Git 1.8中进行了更改。 2 ,所以在现代版本的Git中,甚至在进行第一次提交之前,您可以使用上述命令:

In old versions of Git, the above commands are equivalent to git reset HEAD <file> and git reset HEAD respectively, and will fail if HEAD is undefined (because you haven't yet made any commits in your repo) or ambiguous (because you created a branch called HEAD, which is a stupid thing that you shouldn't do). This was changed in Git 1.8.2, though, so in modern versions of Git you can use the commands above even prior to making your first commit:



在历史中没有任何提交时,git reset(没有选项或参数)用于错误输出,但它现在给你
一个空索引(以匹配不存在的提交甚至没有)。

"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 add'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆