git 如何从git暂存区中删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19730565/
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 17:14:20  来源:igfitidea点击:

How to remove files from git staging area?

git

提问by omega

I made changes to some of my files in my local repo, and then I did git add -Awhich I think added too many files to the staging area. How can I delete all the files from the staging area?

我对本地存储库中的一些文件进行了更改,然后我做了git add -A一些我认为向暂存区添加了太多文件的操作。如何从暂存区删除所有文件?

After I do that, I'll just manually do git add "filename".

完成后,我将手动执行git add "filename".

回答by Ash Wilson

You can unstage files from the index using

您可以使用从索引中取消暂存文件

git reset HEAD -- path/to/file

Just like git add, you can unstage files recursively by directory and so forth, so to unstage everything at once, run this from the root directory of your repository:

就像 一样git add,您可以按目录等递归地取消暂存文件,因此要立即取消暂存所有内容,请从存储库的根目录运行:

git reset HEAD -- .

Also, for future reference, the output of git statuswill tell you the commands you need to run to move files from one state to another.

此外,为了将来参考, 的输出git status将告诉您将文件从一种状态移动到另一种状态需要运行的命令。

回答by Antony Hatchkins

Use

git reset

to unstage all the staged files.

取消暂存所有暂存文件。

回答by Max

If you've already committed a bunch of unwanted files, you can unstage them andtell git to mark them as deleted (without actually deleting them) with

如果您已经提交了一堆不需要的文件,您可以取消暂存它们告诉 git 将它们标记为已删除(而不实际删除它们)

git rm --cached -r .

--cachedtells it to remove the paths from staging and the index without removing the files themselves and -roperates on directories recursively. You can then git addany files that you want to keep tracking.

--cached告诉它从暂存和索引中删除路径而不删除文件本身并-r递归操作目录。然后git add,您可以跟踪要跟踪的任何文件。

回答by Shad

You could use

你可以用

git reset HEAD

then add the specific files you want with

然后添加您想要的特定文件

git add [directory/]filename

回答by Dan Rosenstark

As noted in other answers, you should use git reset. This will undo the action of the git add -A.

正如其他答案中所述,您应该使用git reset. 这将撤消git add -A.

Note:git resetis equivalent to git reset --mixedwhich does this

注意:git reset相当于git reset --mixed执行此操作

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action. [ git reset ]

重置索引但不重置工作树(即,更改的文件被保留但未标记为提交)并报告尚未更新的内容。这是默认操作。[ git重置]

回答by norixxx

Now at v2.24.0 suggests

现在在 v2.24.0 建议

git restore --staged .

to unstage files.

取消暂存文件。

回答by Amit Kaneria

You can reset the staging area in a few ways:

您可以通过以下几种方式重置暂存区:

  1. Reset HEAD and add all necessary files to check-in again as below:

     git reset HEAD ---> removes all files from the staging area
     git add <files, that are required to be committed>
     git commit -m "<commit message>"
     git push 
    
  1. 重置 HEAD 并添加所有必要的文件以再次签入,如下所示:

     git reset HEAD ---> removes all files from the staging area
     git add <files, that are required to be committed>
     git commit -m "<commit message>"
     git push 
    

回答by Akash Bisariya

To remove all files from staging area use -
git reset
To remove specific file use -
git reset "File path"

从暂存区删除所有文件使用 -
git reset
删除特定文件使用 -
git reset "File path"

回答by Ajith

use

git reset HEAD

This will remove all files from staging area

这将从暂存区中删除所有文件

回答by Vlad Bezden

If unwanted files were added to the staging area but not yet committed, then a simple reset will do the job:

如果不需要的文件被添加到暂存区但尚未提交,那么简单的重置就可以完成这项工作:

$ git reset HEAD file
# Or everything
$ git reset HEAD .

To only remove unstaged changes in the current working directory, use:

要仅删除当前工作目录中未暂存的更改,请使用:

git checkout -- .