Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

git - 如何在提交前撤消'git add'?(How do I undo 'git add' before commit?)

I mistakenly added files to Git using the command:

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

git add myfile.txt

I have not yet run git commit .

(我还没有运行git commit 。)

Is there a way to undo this, so these files won't be included in the commit?

(有一种方法可以撤消此操作,以便这些文件不会包含在提交中?)

  ask by paxos1977 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can undo git add before commit with

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

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.

(当在合理的时间内有太多文件不能一一列出时,这可能会派上用场。)

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).

(在旧版本的Git中,上述命令分别等效于git reset HEAD <file>git reset HEAD ,并且如果HEAD未定义(因为尚未在存储库中进行任何提交)或模棱两可(因为创建了一个名为HEAD的分支,这是愚蠢的事情,您不应该这样做)。)

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 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”(没有选项或参数)会出错,但是现在它为您提供了一个空索引(以匹配您甚至没有参与的不存在的提交)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...