撤消“git add <dir>”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4639091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:48:37  来源:igfitidea点击:

Undo “git add <dir>”?

gitgit-reset

提问by neoneye

I mistakenly added files using the command "git add dir". I have not yet run "git commit". Is there a way to remove this dir and everything contained within it from the commit?

我错误地使用命令“git add dir”添加了文件。我还没有运行“git commit”。有没有办法从提交中删除此目录及其中包含的所有内容?

I have tried git reset dir, but it didn't work. Apparently git reset fileis the way to undo it. But I have so many files and so little time.

我试过了git reset dir,但没有奏效。显然git reset file是撤消它的方法。但我有这么多文件和这么少时间。

回答by Aristotle Pagaltzis

To remove a directory and everything inside it from the index,

要从索引中删除目录及其中的所有内容,

git rm --cached -r dir

The --cachedswitch makes git rmoperate on the index only and not touch the working copy. The -rswitch makes it recursive.

--cached开关使git rm该指数操作,而不是触摸工作副本。该-r开关使得递归的。

回答by knittl

You will want to use git rm --cached -r <dir>. this command will remove the staged directory contents from the index.

你会想要使用git rm --cached -r <dir>. 此命令将从索引中删除暂存目录内容。

if the directory was already tracked you have to find new and old files manually and unstage them …

如果目录已经被跟踪,你必须手动查找新旧文件并将它们取消暂存......

Probably run git reset <dir>after that to reset existing (and already tracked) files inside the directory.

可能在此git reset <dir>之后运行以重置目录中的现有(和已跟踪)文件。



Update 2019:

2019 年更新:

Simply run git reset directory, it will unstage all newly added files.

只需运行git reset directory,它将取消暂存所有新添加的文件。

回答by moinudin

Use findand xargs:

使用findxargs

find dir -type f | xargs git reset

回答by mrduncle1

Unstage directory

取消暂存目录

$ git reset <dir>

Unstages all the files and folders I staged with:

取消暂存我暂存的所有文件和文件夹:

$ git add <dir>

Confirm directory unstaged

确认目录未暂存

In order to confirm the removal of the directory and its contents from staging (ie "Changes to be committed"), run the below:

为了确认从暂存中删除目录及其内容(即“要提交的更改”),请运行以下命令:

$ git status

or

或者

$ git diff --staged --name-only 

to confirm that none of the folders or files previously staged are still staged.

确认之前暂存的文件夹或文件均未暂存。

回答by Tofan Jena

One Simple command to remove a staged file/folder, that are not committed yet is - git restore --staged <dir>.

删除尚未提交的暂存文件/文件夹的一个简单命令是 - git restore --staged <dir>

enter image description here

在此处输入图片说明