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.tw
本倉庫將分步在教程中構建。
一個基本的外部樣式表(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