Git Commit
Git Commit
既然我们已经完成了工作,我们就可以将 stage
(暂存区)的内容提交到我们的仓库(repo)中进行 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(不经过暂存)
有时,当您进行小的更改时,使用暂存环境似乎有些浪费时间。可以直接提交更改,跳过暂存环境。 -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 - 已删除的文件
我们看到预期的文件被修改了。那么,让我们直接提交它。
示例
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 日志
要查看仓库的 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!