我们已经成功地添加并提交了一个hello.html文件,现在,是时候继续工作了,于是,我们继续修改hello文件,改成如下内容:
<!DOCTYPE html>
<html>
<head>
<title>hello</title>
</head>
<body>
<p>it's a text for git</p>
</body>
</html>
现在,运行git status
命令看看结果:
$ git status
On branch master
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: hello.html
no changes added to commit (use "git add" and/or "git commit -a")
git status
命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,hello.html被修改过了,但还没有准备提交的修改。
虽然Git告诉我们hello.htmlt被修改了,但如果能看看具体修改了什么内容,自然是很好的。比如你休假两周从国外回来,第一天上班时,已经记不清上次怎么修改的hello.html,所以,需要用git diff
这个命令看看:
$ git diff
diff --git a/hello.html b/hello.html
index 8e56559..c496c3e 100644
--- a/hello.html
+++ b/hello.html
@@ -1,9 +1,9 @@
-<<!DOCTYPE html>
+<!DOCTYPE html>
<html>
<head>
<title>hello</title>
</head>
<body>
- llllll
+ <p>it's a text for git</p>
</body>
</html>
\ No newline at end of file
git diff
顾名思义就是查看difference,显示的格式正是Unix通用的diff格式,可以从上面的命令输出看到,我们把body里面的内容改为it's a text for git 还有html上面少了个括号
知道了对hello.htmlt作了什么修改后,再把它提交到仓库就放心多了,提交修改和提交新文件是一样的两步,第一步是git add
:
$ git add hello.html
同样没有任何输出。在执行第二步git commit
之前,我们再运行git status
看看当前仓库的状态:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.html
git status
告诉我们,将要被提交的修改包括hello.html,下一步,就可以放心地提交了:
$ git commit -m "add distributed"(这里add distributed 写错了 本来应该是对你修改的内容描述 当时看廖雪峰大大写到这 一下子没明白后来才看明白 所以这应该是改为add p element)
[master e310b3c] add distributed
1 file changed, 2 insertions(+), 2 deletions(-)
提交后,我们再用git status
命令看看仓库的当前状态:
$ git status
# On branch master
nothing to commit (working directory clean)
Git告诉我们当前没有需要提交的修改,而且,工作目录是干净(working directory clean)的。
小结一下:
-
要随时掌握工作区的状态,使用
git status
命令。 -
如果
git status
告诉你有文件被修改过,用git diff
可以查看修改内容。