如何在提交前撤消'git add'? [英] How do I undo 'git add' before commit?

查看:381
本文介绍了如何在提交前撤消'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.

您可以使用

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 repository) 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 reset

这篇关于如何在提交前撤消'git add'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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