• git查看历史记录及修改内容


    前言

    Git 中没有真正的方法来做任何事情,这就是它的妙处!比如查看修改内容这件事,有些人会想到 git log,有些人会想到 git show,最近我又学到一个 git whatchanged,实现目的方法多种多样,各种途径任君挑选。

    刚开始步入软件开发行业时喜欢捣鼓各种软件,进行各种个性化配置,任意修改快捷键,这样在开发过程中确实会舒服一些,但是换了一个环境时(电脑重装或在别人的机器),就好像一个什么也不会的傻子一样,所以慢慢的我开始强迫自己熟悉软件自己的快捷键和各种命令,这样在重装系统时会免去很多麻烦,并且因为一些命令用习惯了,在编写部署脚本时也不会总是写出不能识别的简写命令了。

    修改文件

    为了测试各种的查找修改记录的命令,我先进行一次包含增加、修改、删除的提交,然后对比来看各个命令的作用,实际修改如下:

    1. 在文件address.txt中增加两行数据
    2. 清空文件age.txt中4行数据
    3. 修改文件name.txt中一行数据
    4. 增加带有3行数据的phone.txt文件
    5. 删除带有2行数据的story.txt文件

    在执行了 git add . 命令后,可以用 git diff --staged 查看即将提交的文件修改,展示如下:

    $ git diff --staged
    diff --git a/address.txt b/address.txt
    index e69de29..8f9d6e6 100644
    --- a/address.txt
    +++ b/address.txt
    @@ -0,0 +1,2 @@
    +beijing
    +shanghai
    \ No newline at end of file
    diff --git a/age.txt b/age.txt
    index 58f78c9..e69de29 100644
    --- a/age.txt
    +++ b/age.txt
    @@ -1,4 +0,0 @@
    -12
    -16
    -17
    -15
    \ No newline at end of file
    diff --git a/name.txt b/name.txt
    index ac37a53..0ed306a 100644
    --- a/name.txt
    +++ b/name.txt
    @@ -1,4 +1,4 @@
     tom
    -alice
    +jerry
     bily
     andy
    \ No newline at end of file
    diff --git a/phone.txt b/phone.txt
    new file mode 100644
    index 0000000..241bcdb
    --- /dev/null
    +++ b/phone.txt
    @@ -0,0 +1,3 @@
    +110
    +120
    +119
    \ No newline at end of file
    diff --git a/story.txt b/story.txt
    deleted file mode 100644
    index 0d89902..0000000
    --- a/story.txt
    +++ /dev/null
    @@ -1,2 +0,0 @@
    -King
    -Wolf
    \ No newline at end of file
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    关于 git diff 对于很多使用 git 管理代码的小伙伴再熟悉不过了,但是其中有些细节还是需要学习的:

    1. diff --git a/name.txt b/name.txt 这一行是说以下展示 name.txt 文件修改前后的信息,a/name.txtb/name.txt 分别表示修改前后的文件名

    2. index ac37a53..0ed306a 100644 这一行表示文件修改前后的 object100644 表示这是一个常规文件,文件权限644,使用 git cat-file -p可以查看文件内容:

      $ git cat-file -p ac37a53
      tom
      alice
      bily
      andy
      
      $ git cat-file -p 0ed306a
      tom
      jerry
      bily
      andy
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    3. --- a/name.txt表示修改前的文件,+++ b/name.txt表示修改后的文件

    4. @@ -1,4 +1,4 @@ 这一句应该是最有意思的,也是不用一看懂的一行,其中开头和结尾的 @@ 为固定格式,-1,4 表示修改前的 1~4行,+1~4表示修改后的1~4行,这句话的意思就是,以下内容展示了修改前 1~4行到修改后 1~4行的文件变化,因为我们只修改了一行,所以修改前后行数不变,如果新增行数和删除行数不同,那么这个位置展示的行数也是不同的,例如 phone.txt 文件的变化 @@ -0,0 +1,3 @@

    5. 最后就是文件具体的变化了,新增内容前面是加号 +,删除内容前面是减号 -

       tom
      -alice
      +jerry
       bily
       andy
      
      • 1
      • 2
      • 3
      • 4
      • 5

    查询文件修改

    当我们把修改的内容提交以后,这条修改属于存入了仓库的历史之中,git diff 就无法再查看文件的变化了,而需要使用 git loggit show 来查看文件修改的内容,接下来我们来看看这些命令都能用来干嘛。

    git show

    git show 默认展示最近一次提交的修改,与执行 git commit 命令之前的 git diff --staged 查看得到的绝大部分内容相同,只是在开头位置包含最新提交的信息:

    $ git show
    commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
    Author: albert <albert101@163.com>
    Date:   Sun Aug 21 15:22:08 2022 +0800
    
        update example data
    
    diff --git a/address.txt b/address.txt
    index e69de29..8f9d6e6 100644
    --- a/address.txt
    +++ b/address.txt
    @@ -0,0 +1,2 @@
    +beijing
    +shanghai
    \ No newline at end of file
    diff --git a/age.txt b/age.txt
    index 58f78c9..e69de29 100644
    --- a/age.txt
    +++ b/age.txt
    @@ -1,4 +0,0 @@
    -12
    -16
    -17
    -15
    \ No newline at end of file
    diff --git a/name.txt b/name.txt
    index ac37a53..0ed306a 100644
    --- a/name.txt
    +++ b/name.txt
    @@ -1,4 +1,4 @@
     tom
    -alice
    +jerry
     bily
     andy
    \ No newline at end of file
    diff --git a/phone.txt b/phone.txt
    new file mode 100644
    index 0000000..241bcdb
    --- /dev/null
    +++ b/phone.txt
    @@ -0,0 +1,3 @@
    +110
    +120
    +119
    \ No newline at end of file
    diff --git a/story.txt b/story.txt
    deleted file mode 100644
    index 0d89902..0000000
    --- a/story.txt
    +++ /dev/null
    @@ -1,2 +0,0 @@
    -King
    -Wolf
    \ No newline at end of file
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    git show 还可以加数字,比如 git show -3 就是展示最近3次提交修改信息。

    git show --stat 可以查看最新提交的修改文件,如果想查看指定提交的修改文件信息,可以在后面跟上commit-id,例如 git show --stat 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee

    $ git show --stat
    commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
    Author: albert <albert101@163.com>
    Date:   Sun Aug 21 15:22:08 2022 +0800
    
        update example data
    
     address.txt | 2 ++
     age.txt     | 4 ----
     name.txt    | 2 +-
     phone.txt   | 3 +++
     story.txt   | 2 --
     5 files changed, 6 insertions(+), 7 deletions(-)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这个展示信息可以清楚的看出哪些文件新增了内容,哪些文件删除了内容,并且展示了文件增伤行数的比例,但是有一点它不够清晰,那就是无法看出哪些是新增的文件,哪些是删除的文件,比如 age.txtstory.txt 都显示删除了数据,但实际上 story.txt 整个文件都从仓库中删除了,要想看出文件增删状态可以使用接下来展示的这个命令 git whatchanged

    git whatchanged

    git whatchanged 可以展示出文件的增删状态和权限修改,默认分页展示所有提交记录,可以后面加数字来展示最近几次的文件增删状态:

    $ git whatchanged -1
    commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
    Author: albert <albert101@163.com>
    Date:   Sun Aug 21 15:22:08 2022 +0800
    
        update example data
    
    :100644 100644 e69de29 8f9d6e6 M        address.txt
    :100644 100644 58f78c9 e69de29 M        age.txt
    :100644 100644 ac37a53 0ed306a M        name.txt
    :000000 100644 0000000 241bcdb A        phone.txt
    :100644 000000 0d89902 0000000 D        story.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    从这个文件中就可以看出 address.txt、age.txt、name.txt 三个文件被修改了,phone.txt 是新增加的,story.txt 文件被删除了,如果在命令后面加上选项 --stat 作用就和 git show 一样了,兜兜转转回到原点~

    $ git whatchanged -1 --stat
    commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
    Author: albert <albert101@163.com>
    Date:   Sun Aug 21 15:22:08 2022 +0800
    
        update example data
    
     address.txt | 2 ++
     age.txt     | 4 ----
     name.txt    | 2 +-
     phone.txt   | 3 +++
     story.txt   | 2 --
     5 files changed, 6 insertions(+), 7 deletions(-)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    git log

    根据我个人的理解,git show 注重查看一次提交中修改的内容,而 git log 主要用于查找历史提交的脉络,但这不是绝对的,因为git做一件事,没有绝对的一种方式,你也可以用 git log 来实现 git show

    • 展示最近一次提交

      $ git log -1
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 展示其他分支最近一次提交

      $ git log -1 dev
      commit 62cc52cbc7f9581fa825b443aba3481083459656 (dev)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 12:02:11 2022 +0800
      
          init git repository
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 展示修改的文件列表及文件修改的统计

      $ git log -1 --stat
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
       address.txt | 2 ++
       age.txt     | 4 ----
       name.txt    | 2 +-
       phone.txt   | 3 +++
       story.txt   | 2 --
       5 files changed, 6 insertions(+), 7 deletions(-)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    • 展示每次修改的文件列表

      $ git log -1 --name-only
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
      address.txt
      age.txt
      name.txt
      phone.txt
      story.txt
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    • 展示修改的文件列表和显示状态

      $ git log -1 --name-status
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
      M       address.txt
      M       age.txt
      M       name.txt
      A       phone.txt
      D       story.txt
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    • 展示指定作者提交的记录

      $ git log -1 --author="albert"
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • 单行展示提交的记录

      $ git log --oneline
      2447e2b (HEAD -> master) update example data
      62cc52c (dev) init git repository
      
      • 1
      • 2
      • 3
    • 展示指定日期之前的提交记录

      $ git log --before='2022-08-22'
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
      commit 62cc52cbc7f9581fa825b443aba3481083459656 (dev)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 12:02:11 2022 +0800
      
          init git repository
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    • 展示一天之内的提交记录

      $ git log --since=1.day.ago
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
      commit 62cc52cbc7f9581fa825b443aba3481083459656 (dev)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 12:02:11 2022 +0800
      
          init git repository
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    • 展示指定包含指定内容的提交记录

      $ git log --grep=update
      commit 2447e2b9c15472f2ead7bf451aa5fc9c3f34f5ee (HEAD -> master)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 15:22:08 2022 +0800
      
          update example data
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    • 展示指定不包含指定内容的提交记录

      $ git log --grep=update --invert-grep
      commit 62cc52cbc7f9581fa825b443aba3481083459656 (dev)
      Author: albert <albert101@163.com>
      Date:   Sun Aug 21 12:02:11 2022 +0800
      
          init git repository
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    • 终极大招,图形化展示,其实用的并不多

      $ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative --all
      * 2447e2b - (HEAD -> master) update example data (77 minutes ago) <albert>
      * 62cc52c - (dev) init git repository (2 hours ago) <albert>
      *   719ec7a - (refs/stash) WIP on master: 83f00c5 init git repository (2 hours ago) <albert>
      |\
      | * 9c87e06 - index on master: 83f00c5 init git repository (2 hours ago) <albert>
      |/
      * 83f00c5 - init git repository (5 hours ago) <albert>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

    总结

    • 查看最近一次修改的内容使用 git show
    • 查看最近一次修改的文件使用 git show --statgit log -1 --name-only
    • 查看最近一次修改的文件状态使用 git whatchanged -1git log -1 --name-status
    • 图形化显示git提交记录使用 git log --graph

    ==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==

    轻生的人到底是勇敢还是懦弱呢?虽说我未经历过他的人生不该轻易评判,但就我主观来思考这类事,大抵是因为对生活失去了信心。人固有一死,或重于泰山,或轻于鸿毛。而遇到困难就选择轻生的人是自私且不负责任的,若你无牵无挂走了也就走了,倘若还有家人岂不是还要拿出额外一笔钱给你办个葬礼?连死都不怕了,还有什么是过不去的呢?

  • 相关阅读:
    CH552T可以这样接si24r1吗
    10min快速回顾C++语法(八)STL专题
    打码平台之图鉴的使用步骤
    单个Dockerfile合并多个镜像以及docker-compose批量部署
    Window 2016 + VMWare +Thingworx 8.5 安装总结
    欧几里得算法及相关扩展算法
    腾讯云服务器按量付费如何转为包年包月?
    MATLAB R2024a 主要更新内容
    OpenCV_06 图像平滑:图像噪声+图像平滑+滤波
    CDH大数据平台 31Cloudera Manager Console之impala hive负载均衡(markdown新版)
  • 原文地址:https://blog.csdn.net/shihengzhen101/article/details/126455725