Git 暂存环境
Git 暂存环境
Git 的核心功能之一是暂存环境和提交(Commit)的概念。
在你工作时,你可能会添加、编辑和删除文件。但每当你达到一个里程碑或完成一部分工作时,你应该将文件添加到暂存环境。
暂存 (Staged) 的文件是准备好被提交 (committed) 到你正在工作的仓库中的文件。你很快就会学到更多关于 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 教程
这是 https://w3schools.org.cn 上的 Git 教程的一个示例仓库
本仓库在教程中逐步构建。
一个基本的外部样式表(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
而不是单独的文件名,将 stage
所有更改(新建、修改和删除)的文件。
示例
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 个文件都已添加到暂存环境,我们已准备好进行第一次 commit
。
注意: git add --all
的简短命令是 git add -A