Git 暂存区
Git 暂存区
Git 的核心功能之一是暂存区(Staging Environment)和提交(Commit)的概念。
在工作过程中,你可能会添加、编辑和删除文件。但每当你达到一个里程碑或完成一部分工作时,你应该将文件添加到暂存区。
暂存的文件是准备好被提交到你正在工作的仓库中的文件。你将在稍后了解更多关于 commit
的信息。
现在,我们已经完成了 index.html
的工作。所以我们可以将其添加到暂存区。
示例
git add index.html
文件应该被暂存。让我们检查一下状态:
示例
git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
现在文件已被添加到暂存区。
Git 添加多个文件
您也可以一次暂存多个文件。让我们在工作文件夹中添加 2 个文件。再次使用文本编辑器。
一个 README.md
文件,描述了仓库(建议所有仓库都这样做)
示例
# hello-world
Hello World 仓库,用于 Git 教程
这是 W3schools.com 上的 Git 教程的示例仓库 https://w3schools.org.cn
本仓库将分步在教程中构建。
一个基本的外部样式表(bluestyle.css
)
示例
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
然后更新 index.html
以包含样式表
示例
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>
<h1>你好,世界!</h1>
<p>This is the first file in my new Git Repo.</p>
</body>
</html>
现在将当前目录下的所有文件添加到暂存区
示例
git add --all
使用 --all
而不是单独的文件名将暂存
所有更改(新添加、已修改和已删除)的文件。
示例
git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
new file: bluestyle.css
new file: index.html
现在所有 3 个文件都已添加到暂存区,我们已准备好进行第一次提交
。
注意: git add --all
的简写命令是 git add -A