Git Commit
Git Commit
既然我們已經完成了工作,我們就可以將我們的倉庫從 stage
轉移到 commit
了。
新增 commit 可以跟蹤我們工作過程中的進度和更改。Git 將每個 commit
視為一個更改點或“儲存點”。這是一個專案的節點,如果你發現一個 bug,或者想做一個更改,都可以回到這個節點。
當我們 commit
時,我們應該始終包含一個訊息。
透過為每個 commit
新增清晰的訊息,你可以(以及其他人)輕鬆地看到什麼在何時發生了變化。
示例
git commit -m "First release of Hello World!"
[master (root-commit) 221ec6e] First release of Hello World!
3 files changed, 26 insertions(+)
create mode 100644 README.md
create mode 100644 bluestyle.css
create mode 100644 index.html
commit
命令執行一個提交,而 -m "message"
則新增一條訊息。
暫存環境已提交到我們的倉庫,訊息為:
"Hello World! 的第一個版本!"
Git Commit(不帶暫存)
有時,當你做小改動時,使用暫存環境似乎有點浪費時間。可以直接 commit 更改,跳過暫存環境。`-a` 選項會自動暫存所有已更改但已跟蹤的檔案。
讓我們給 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>
<p>A new line in our file!</p>
</body>
</html>
然後檢查我們倉庫的狀態。但這次,我們將使用 --short 選項以更緊湊的方式檢視更改。
示例
git status --short
M index.html
注意: 短狀態標誌為:
- ?? - 未跟蹤的檔案
- A - 已新增到暫存區的檔案
- M - 已修改的檔案
- D - 已刪除的檔案
我們看到了預期的檔案已被修改。所以讓我們直接 commit 它。
示例
git commit -a -m "Updated index.html with a new line"
[master 09f4acd] Updated index.html with a new line
1 file changed, 1 insertion(+)
警告: 通常不建議跳過暫存環境。
跳過暫存步驟有時會導致你包含不需要的更改。
Git Commit Log(提交日誌)
要檢視倉庫的 commit 歷史,可以使用 log
命令。
示例
git log
commit 09f4acd3f8836b7f6fc44ad9e012f82faf861803 (HEAD -> master)
Author: w3schools-test <test@w3schools.com>
Date: Fri Mar 26 09:35:54 2021 +0100
Updated index.html with a new line
commit 221ec6e10aeedbfd02b85264087cd9adc18e4b26
Author: w3schools-test <test@w3schools.com>
Date: Fri Mar 26 09:13:07 2021 +0100
First release of Hello World!