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!