github初探(三)

这次讲一些git bash有用的几个命令,git log查看提交,git tag贴标签和git diff查看提交改动

git log

git log [<options>] [<since>..<until>] [[--] <path>…]

git log默认会列出当前branch的历史提交记录,包括SHA-1校验和、提交作者、时间以及注释

 1 icefox@ICEFOX-PC /d/JavaScript/My Plugin/jquery-smartAt (master)
 2 $ git log
 3 commit 6c539aadc3125c04b38ab56e56cd454b5d5bfb8e
 4 Author: icefox <icefox0801@hotmail.com>
 5 Date:   Sun May 5 01:15:59 2013 +0800
 6 
 7     add .gitattributes and .gitignore
 8 
 9 commit 4aea315fe7b9ee43fce27fc1448ab02488a3bb7e
10 Author: icefox <icefox0801@hotmail.com>
11 Date:   Thu May 2 14:21:45 2013 +0800
12 
13     bug fix
14 :

输入回车则会多显示一行,输入'q'退出。

常用操作:

git log -n: 列出最近的n次提交记录

git log --oneline: 以单行形式列出提交记录,默认只有提交的简写hash码和注释

git log -p: 列出提交记录的详细改动

git log -author=[authorName]: 列出某一作者的提交记录

git log --pretty[=<format>]: 以指定格式列出提交记录,<format>可以是onelineshortmediumfullfulleremailraw字符串之一或者是自定义的格式,我们甚至可以为占位符指定颜色,例如

1 $ git log --pretty="%h %Cgreen%s %Creset%ar"
2 6c539aa add .gitattributes and .gitignore 21 hours ago
3 4aea315 bug fix 3 days ago
4 0adee72 Update README.md 4 days ago

%C-reset是恢复默认颜色,这里,<format>中的占位符很多,就不详述了,常用的有%h-提交的简写hash码,%an-提交的作者,%ar-提交的相对日期,%s-提交的注释,等等。

git log -- <path>:列出某一文件或者路径下的提交,例如git log -- index.html会列表所有改动index.html的提交

git log [<since>..<until>]:列出<since>和<until>之间的提交记录,<since>和<until>可以是提交的简写hash码,也可以是分支名称

git tag

git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]
        <tagname> [<commit> | <object>]
git tag -d <tagname>…
git tag [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
        [--column[=<options>] | --no-column] [<pattern>…]
        [<pattern>…]
git tag -v <tagname>…

git tag用于向git仓库加入标签,一般用于加入版本信息,比如我们可以为一个git仓库加上"v0.1"的标签

1 icefox@ICEFOX-PC /d/JavaScript/My Plugin/jquery-smartAt (master)
2 $ git tag -a 'v0.1' -m 'v0.1'

加上v0.1标签以后,那么之前所有的提交都会在v0.1标签下了,而加上v0.1标签时间节点之后的所有提交都不会在v0.1下,比如

1 icefox@ICEFOX-PC /d/JavaScript/My Plugin/jquery-smartAt (master)
2 $ vim test.html
3 icefox@ICEFOX-PC /d/JavaScript/My Plugin/jquery-smartAt (master)
4 $ git add test.html
5 icefox@ICEFOX-PC /d/JavaScript/My Plugin/jquery-smartAt (master)
6 $ git commit -a -m 'for git tag test'
7 [master 423cae6] for git tag test
8  1 file changed, 2 insertions(+)
9  create mode 100644 test.html

我们添加了一个test.html并提交,此时git log v0.1,发现并没有'for git tag test'这次提交,这时我们再加上一个v0.2的标签

1 $ git tag -a 'v0.2' -m 'v0.2'

再git log v0.2,发现v0.2的标签中包含'for git tag test'这次提交。最后,为了查看v0.1到v0.2版本升级的过程中我们提交了哪些内容

1 icefox@ICEFOX-PC /d/JavaScript/My Plugin/jquery-smartAt (master)
2 $ git log v0.1..v0.2 --oneline
3 423cae6 for git tag test

可以看到,只有'for git tag test'这次提交。这样,我们在git项目开发的过程中,就可以在重要提交的时间节点上为项目添加上标签,这样就实现了加入版本信息的功能。

常用操作:

git tag -d <tagname>: 删除本地仓库中的标签,若想删除远程git仓库中的标签,请使用git push origin :refs/tags/<tagname>

git tag -l: 列出本地仓库的所有标签

git tag -a <tagname> <commit>: 某历史的某次提交加上标签,一般用于后期贴标签

git diff

git diff [options] [<commit>] [--] [<path>…]
git diff [options] --cached [<commit>] [--] [<path>…]
git diff [options] <commit> <commit> [--] [<path>…]
git diff [options] <blob> <blob>
git diff [options] [--no-index] [--] <path> <path>

git diff用以查看两次提交之间或者某次提交与当前工作目录的差别,默认是查看上次提交和当前工作目录的差别

 1 icefox@ICEFOX-PC /d/JavaScript/My Plugin/jquery-smartAt (master)
 2 $ git diff
 3 diff --git a/index.html b/index.html
 4 index eb558a6..20dddfa 100644
 5 --- a/index.html
 6 +++ b/index.html
 7 @@ -1,4 +1,4 @@
 8 -<!DOCTYPE html>
 9 +new insert<!DOCTYPE html>
10  <html>
11  <head>
12  <meta charset="utf-8" />

常用操作:

git diff <commit>: 比较某次提交和当前工作目录的差别,HEAD代表最后一次的提交,HAED~1代表倒数第二次,依次类推。

git diff <commit> <commit>: 比较两次提交之间的变动

posted @ 2013-05-06 00:24  FingerDancing  阅读(1234)  评论(1编辑  收藏  举报