本文翻译自:How to remove files from git staging area?

I made changes to some of my files in my local repo, and then I did git add -A which I think added too many files to the staging area. 我对本地存储库中的某些文件进行了更改,然后我进行了git add -A ,我认为这会在过渡区域中添加了太多文件。 How can I delete all the files from the staging area? 如何从暂存区删除所有文件?

After I do that, I'll just manually do git add "filename" . 完成之后,我将手动执行git add "filename"


#1楼

参考:https://stackoom.com/question/1Kmov/如何从git临时区域删除文件


#2楼

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 status will tell you the commands you need to run to move files from one state to another. 另外,为了将来参考, git status的输出将告诉您将文件从一种状态转移到另一种状态所需的命令。


#3楼

If you've already committed a bunch of unwanted files, you can unstage them and tell git to mark them as deleted (without actually deleting them) with 如果您已经提交了许多不需要的文件,则可以取消暂存它们, 告诉git将它们标记为已删除(实际上并没有删除它们),

git rm --cached -r .

--cached tells it to remove the paths from staging and the index without removing the files themselves and -r operates on directories recursively. --cached告诉它从暂存和索引中删除路径,而不删除文件本身, -r递归地处理目录。 You can then git add any files that you want to keep tracking. 然后,您可以git add要跟踪的任何文件。


#4楼

You could use 你可以用

git reset HEAD

then add the specific files you want with 然后添加所需的特定文件

git add [directory/]filename

#5楼

Use 采用

git reset

to unstage all the staged files. 取消所有已暂存的文件。


#6楼

As noted in other answers, you should use git reset . 如其他答案所述,您应该使用git reset This will undo the action of the git add -A . 这将撤消git add -A

Note: git reset is equivalent to git reset --mixed which does this 注意: git reset等同于git reset --mixed

Resets the index but not the working tree (ie, 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 reset ]

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐