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.tw 上的 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