从暂存区移除文件

GitGitBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在 Git 中,暂存区是文件在提交到仓库之前准备更改的地方。有时,你可能会不小心将不想提交的文件添加到暂存区。在本实验中,你将学习如何使用 Git 从暂存区中移除文件。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/DataManagementGroup(["`Data Management`"]) git/DataManagementGroup -.-> git/restore("`Revert Files`") subgraph Lab Skills git/restore -.-> lab-12766{{"`从暂存区移除文件`"}} end

从暂存区移除文件

你正在 git-playground 仓库中处理一个项目。你已经对文件做了一些更改,并使用 git add 命令将它们添加到了暂存区。然而,你意识到自己不小心添加了一个你不想提交的文件。你需要将这个文件从暂存区移除。

  1. 查看当前工作目录状态:
git status
  1. 使用 git restore --staged 命令将 newfile.txt 文件从暂存区移除:
git restore --staged newfile.txt
  1. 使用 git status 命令验证该文件是否已从暂存区移除:
git status

这是最终结果:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md

Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile.txt

总结

在本实验中,你学习了如何使用 git restore --staged 命令从暂存区移除文件。当你不小心将不想提交的文件添加到暂存区时,这会很有用。记得使用 git status 命令来验证更改是否已从暂存区移除。

您可能感兴趣的其他 Git 教程