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

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

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 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 重置"(不带选项或参数)用于在以下情况下出错您的历史记录中没有任何提交,但它现在给了您一个空索引(为了匹配你甚至不在的不存在的提交).

"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天全站免登陆