这节课主要介绍下本地目录的文件修改后如何添加到暂存区、提交到 Git 版本库,以及如何比较文件内容差异、如何跳过添加暂存区步骤提交等内容。大家也发现了,提交的操作我们前几节提到过,当时可能没有太多概念,这节我们就来个系统讲解,补充强化之前学过的内容。
第一次克隆存储库时,所有文件都会被跟踪,因为是从 Git 上拉取下来的,Git 当然知道。
1. 查看文件状态
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
$ echo 'test status' > test2.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
test2.txt
nothing added to commit but untracked files present (use "git add" to track)
附:演示流程如下:
上面的操作我们也看到了,当新增了一个文件之后,使用 "git status" 命令查看文件状态信息时,最后一行内容:nothing added to commit but untracked files present (use “git add” to track)。提示我们没有提交任何内容但是存在未跟踪的文件,括号里面说可以使用 “git add” 命令去使其被跟踪管理。
$ git add test2.txt
之后再查看下文件状态:
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test2.tx
直到这里,一切都是那么顺滑,不对,顺利!
附:演示流程如下:
3. 将暂存区内容提交
接着上一点,我们准备将 test2.txt 进行正式提交。
但是,但是,但是,我就不提交(微笑脸)。
直接提交岂不是太简单了?现在我在提交之前又反悔了,突然想起来还想加一句 “hello world”,那就加呗,加完我们再看看会变成什么鬼样子。
$ echo 'hello world' >> test2.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test2.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: test2.
哎!不对啊。test2.txt 这个文件刚刚不是在 “Changes to be committed” 下面吗?现在怎么同时又存在于 “Changes not staged for commit” 下面了。这个文件怎么同时存在 “已暂存” 和 “未暂存” 两种状态了?
附:这部分演示流程如下:
别急,跟我往下继续看!那要不我们先执行下提交看看会变成什么状态?好!说干就干!对了,忘了说了,提交命令是 “git commit -m ‘这是提交说明’”,hiahia~~~
$ git commit -m 'commit 1'
[master bba6ffb] commit 1
1 file changed, 1 insertion(+)
create mode 100644 test2.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
仔细看:我在执行 "git commit" 之后,提示 test2.txt 已提交。但是我执行 “git status” 之后,却发现在 “Changes not staged for commit” 部分还存在一个 test2.txt,这是为啥?
其实,提交后只把前面步骤中执行了 “git add” 那部分修改进行了提交,而后面我再追加的 “hello world” 的内容由于没有执行 “git add”,并没有纳入 git 的暂存区,提交也就自然提交不了。那想要再提交这部分内容怎么办?简单啊!继续执行 “git add”,“git commit” 就好了。
$ git add test2.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: test2.txt
$ git commit -m 'commit 2'
[master 446c174] commit 2
1 file changed, 1 insertion(+)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
看,现在状态正常了。是不是一切都是那么熟悉?就像初恋般的感觉。
附:这部分演示流程如下:
4. 比较工作区和版本库的文件差异
$ git diff HEAD -- test.txt
5. 跳过暂存区直接提交
$ git commit -a -m '备注'
演示流程如下:
正常的开发过程中,对于提交日志的规范都有要求。日志是我们作为历史版本维护的一个很重要的内容,一个好的日志模板可以方便进行代码 review,以及后续开发者快速了解历史问题等等。本教程只是演示,实际开发中请遵守具体规范!
6. 总结
温故而知新,我们先走起。本节课内容总结如下:
好了,今天的内容我们就讲到这里,希望大家多加练习,早日 get 到 git 的精髓!