Git笔记 (3)

执行git commit --amend会打开编辑器(一般是vim),你可以修改提交说明(i进入插入模式修改),然后保存退出即可(Esc,然后shift+:,打wq,最后enter)

5.3.1 git status

知道文件的状态,我们通过一些实例来学习一下。在playground仓库新建hello.py,然后加入下面一行:

print("hello everyone")

git status命令会显示当前仓库的文件状态,在终端中输入git status,显示如下内容:

$ 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) hello.py nothing added to commit but untracked files present (use "git add" to track)

可以看到我们刚刚新建的hello.py处于untracked files。然后我们在终端中输入git add hello.py,然后我们再次输入git status,会出现下面内容:

$ git add hello.py warning: LF will be replaced by CRLF in hello.py. The file will have its original line endings in your working directory. $ 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: hello.py

可以看到hello.py的状态变为Changes to be committed,这意味着hello.py进入了暂存区,我们再用git commit提交这次更改,然后再用git status查看状态,结果如下:

$ git commit -m "add hello everyone" [master 405cd1b] add hello everyone 1 file changed, 1 insertion(+) create mode 100644 hello.py $ git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean

可以看到显示nothing to commit,文件状态处于未更改状态,因为我们完成了这一次版本的提交。最后我们可以使用git push上传我们这次的提交到远程github服务器上。最后可以看到github的仓库中多了我们刚才修改的hello.py.

5.3.2 git diff

git status命令的输出可能过于模糊,如果你想知道具体修改了什么地方,可以用git diff 命令。它用来回答两个问题:当前做的哪些更新还没有暂存? 有哪些更新已经暂存起来准备好了下次提交?代码修改运行出错,有时候可以用git diff,可以看到自己或他人新加入了那些行,有助于修复bug和多人合作。
我们在hello.py文件再加入一行:

print("hello fang")

然后在终端中输入git diff,结果如下:

$ git diff warning: LF will be replaced by CRLF in hello.py. The file will have its original line endings in your working directory. diff --git a/hello.py b/hello.py index 1ad4063..d469b07 100644 --- a/hello.py +++ b/hello.py @@ -1 +1,2 @@ print("hello everyone") +print("hello yang")

+号代表新添加的行,然后我们再加入一行:

print("hello fang")

然后再调用git diff命令,结果如下:

$ git diff diff --git a/hello.py b/hello.py index 97d6fb2..2595b9c 100644 --- a/hello.py +++ b/hello.py @@ -1 +1,3 @@ print("hello everyone") +print("hello yang") +print("hello fang")

可以看到我们又多加了一行,我们将文件状态转到暂存区(使用git add hello.py)看看,然后运行git diff,可以看到是没有任何输出的,因为git diff 本身只显示尚未暂存的改动。如果想看暂存前后的变化,则需要使用git diff --cached命令,则可以看到:

$ git diff --cached diff --git a/hello.py b/hello.py index 97d6fb2..2595b9c 100644 --- a/hello.py +++ b/hello.py @@ -1 +1,3 @@ print("hello everyone") +print("hello yang") +print("hello fang")

这里提一句,有时候我们运行git status,我们会看到一个文件显示两种状态,比如我们上面加了一行print("hello FY"),然后运行git status,可以看到如下结果:

$ 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: hello.py 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.py

出现这种结果的原因是因为我们的文件确实存在两种状态,一种是我们之前加入到暂存区,一种是我们刚刚修改添加的,两者是可以共存的。

5.3.3 git log

git log命令会列出每个提交的SHA-1 校验和、作者的名字和电子邮件地址、提交时间以及提交说明。我们已经提交了许多次了,在bash中输入git log,出现以下内容:

$ git log commit a9b674c88eba0043f84aec4668215358f99c5572 (HEAD -> master) Author: FangYang970206 <15270989505@163.com> Date: Mon Aug 20 16:04:09 2018 +0800 add some info commit 2ae267c61261b6041d16133cca56f4c8155d73fa Author: FangYang970206 <15270989505@163.com> Date: Mon Aug 20 10:02:57 2018 +0800 fix one error commit 405cd1bd4b9e0d19f1698c7f7cb8f77184424040 (origin/master, origin/HEAD) Author: FangYang970206 <15270989505@163.com> Date: Sun Aug 19 21:49:29 2018 +0800 add hello everyone commit edb1d60a14b25be099205a62a7c469083dc1338a Author: FangYang970206 <15270989505@163.com> Date: Fri Aug 17 17:41:25 2018 +0800 first commit

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppgpw.html