• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
isuning
博客园    首页    新随笔    联系   管理    订阅  订阅
Git系列教程 查看提交历史&标签管理之六

Git 查看提交历史

Git 提交历史一般常用两个命令:

  • git log - 查看历史提交记录。

  • git blame - 以列表形式查看指定文件的历史修改记录。

  • git log

  • 在使用 Git 提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用 git log 命令查看。

  • 针对我们前一章节的操作,使用 git log 命令列出历史提交记录如下:

  • $ git log
    commit d5e9fc2c811e0ca2b2d28506ef7dc14171a207d9 (HEAD -> master)
    Merge: c68142b 7774248
    Author: runoob <test@runoob.com>
    Date:   Fri May 3 15:55:58 2019 +0800
    
        Merge branch 'change_site'
    
    commit c68142b562c260c3071754623b08e2657b4c6d5b
    Author: runoob <test@runoob.com>
    Date:   Fri May 3 15:52:12 2019 +0800
    
        修改代码
    
    commit 777424832e714cf65d3be79b50a4717aea51ab69 (change_site)
    Author: runoob <test@runoob.com>
    Date:   Fri May 3 15:49:26 2019 +0800
    
        changed the runoob.php
    
    commit c1501a244676ff55e7cccac1ecac0e18cbf6cb00
    Author: runoob <test@runoob.com>
    Date:   Fri May 3 15:35:32 2019 +0800
    
  • 我们可以用 --oneline 选项来查看历史记录的简洁的版本。

  • $ git log --oneline
    $ git log --oneline
    d5e9fc2 (HEAD -> master) Merge branch 'change_site'
    c68142b 修改代码
    7774248 (change_site) changed the runoob.php
    c1501a2 removed test.txt、add runoob.php
    3e92c19 add test.txt
    3b58100 第一次版本提交
    
  • 这告诉我们的是,此项目的开发历史。

  • 我们还可以用 --graph 选项,查看历史中什么时候出现了分支、合并。以下为相同的命令,开启了拓扑图选项:

  • *   d5e9fc2 (HEAD -> master) Merge branch 'change_site'
    |\  
    | * 7774248 (change_site) changed the runoob.php
    * | c68142b 修改代码
    |/  
    * c1501a2 removed test.txt、add runoob.php
    * 3e92c19 add test.txt
    * 3b58100 第一次版本提交
    
  • 现在我们可以更清楚明了地看到何时工作分叉、又何时归并。

  • 你也可以用 –reverse 参数来逆向显示所有日志。

  • $ git log --reverse --oneline
    3b58100 第一次版本提交
    3e92c19 add test.txt
    c1501a2 removed test.txt、add runoob.php
    7774248 (change_site) changed the runoob.php
    c68142b 修改代码
    d5e9fc2 (HEAD -> master) Merge branch 'change_site'
    
  • 如果只想查找指定用户的提交日志可以使用命令:git log --author , 例如,比方说我们要找 Git 源码中 Linus 提交的部分:

  • $ git log --author=Linus --oneline -5
    81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory
    3bb7256 make "index-pack" a built-in
    377d027 make "git pack-redundant" a built-in
    b532581 make "git unpack-file" a built-in
    112dd51 make "mktag" a built-in
    
  • 如果你要指定日期,可以执行几个选项:–since 和 --before,但是你也可以用 --until 和 --after。

  • 例如,如果我要看 Git 项目中三周前且在四月十八日之后的所有提交,我可以执行这个(我还用了 --no-merges 选项以隐藏合并提交):

  • $ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
    5469e2d Git 1.7.1-rc2
    d43427d Documentation/remote-helpers: Fix typos and improve language
    272a36b Fixup: Second argument may be any arbitrary string
    b6c8d2d Documentation/remote-helpers: Add invocation section
    5ce4f4e Documentation/urls: Rewrite to accomodate transport::address
    00b84e9 Documentation/remote-helpers: Rewrite description
    03aa87e Documentation: Describe other situations where -z affects git diff
    77bc694 rebase-interactive: silence warning when no commits rewritten
    636db2c t3301: add tests to use --format="%N"
    
  • 更多 git log 命令可查看:http://git-scm.com/docs/git-log

  • git blame

  • 如果要查看指定文件的修改记录可以使用 git blame 命令,格式如下:

  • git blame <file>
    
  • git blame 命令是以列表形式显示修改记录,如下实例:

  • $ git blame README 
    ^d2097aa (tianqixin 2020-08-25 14:59:25 +0800 1) # Runoob Git 测试
    db9315b0 (runoob    2020-08-25 16:00:23 +0800 2) # 菜鸟教程 
    

Git 标签

如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签。

比如说,我们想为我们的 runoob 项目发布一个"1.0"版本。 我们可以用 git tag -a v1.0 命令给最新一次提交打上(HEAD)"v1.0"的标签。

-a 选项意为"创建一个带注解的标签"。 不用 -a 选项也可以执行的,但它不会记录这标签是啥时候打的,谁打的,也不会让你添加个标签的注解。 我推荐一直创建带注解的标签。

$ git tag -a v1.0 

当你执行 git tag -a 命令时,Git 会打开你的编辑器,让你写一句标签注解,就像你给提交写注解一样。

现在,注意当我们执行 git log --decorate 时,我们可以看到我们的标签了:

*   d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\  
| * 7774248 (change_site) changed the runoob.php
* | c68142b 修改代码
|/  
* c1501a2 removed test.txt、add runoob.php
* 3e92c19 add test.txt
* 3b58100 第一次版本提交

如果我们忘了给某个提交打标签,又将它发布了,我们可以给它追加标签。

例如,假设我们发布了提交 85fc7e7(上面实例最后一行),但是那时候忘了给它打标签。 我们现在也可以:

$ git tag -a v0.9 85fc7e7
$ git log --oneline --decorate --graph
*   d5e9fc2 (HEAD -> master) Merge branch 'change_site'
|\  
| * 7774248 (change_site) changed the runoob.php
* | c68142b 修改代码
|/  
* c1501a2 removed test.txt、add runoob.php
* 3e92c19 add test.txt
* 3b58100 (tag: v0.9) 第一次版本提交

如果我们要查看所有标签可以使用以下命令:

$ git tag
v0.9
v1.0

指定标签信息命令:

git tag -a <tagname> -m "runoob.com标签"

PGP签名标签命令:

git tag -s <tagname> -m "runoob.com标签"
posted on 2022-05-01 22:17  吕布辕门  阅读(129)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3