git命令行查看某个文件的修改历史

这篇博客介绍了如何通过Git命令行工具查看特定文件的修改历史。通过输入`git log -p <文件名>`,可以显示每个提交的详细信息,包括作者、日期和具体的代码改动。将输出重定向到文件方便后续查看。这些提交信息包含了每次更新的内容摘要和具体的代码差异。此方法适用于Git操作不那么直观的情况,帮助开发者追溯代码变更历程。
摘要由CSDN通过智能技术生成

背景

有时在AS中右击文件行号左侧空白处查看修改历史,但此时Annotate是灰色的,这时就需要用git命令进行查看了

方法

假设要查看的文件是build.gradle,则对应的命令就为

➜  first git:(szc) git log -p build.gradle

结果如下图所示

输出内容持久化

我们可以把结果重定向到一个文件中,这样查看起来更方便

➜  first git:(szc) git log -p build.gradle > ../output.txt

 cat一下输出文件即可

➜  first git:(szc) cat ../output.txt
commit f524eeda210b6d2e92dcd43dc13f1831d5bcf94c
Author: songzeceng <392004248@qq.com>
Date:   Thu Dec 12 21:08:09 2019 +0800


    KeyStore加解密


diff --git a/build.gradle b/build.gradle
index 8f91621..20ea27a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -7,7 +7,7 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.5.2'
+        classpath 'com.android.tools.build:gradle:3.5.3'
//        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
         // protobuf插件,AS3.0需要protobuf版本在0.8.2及以上




commit 282b486ac2fa2df7edb0c9ebabc47b6491b32634
Author: songzeceng <392004248@qq.com>
Date:   Wed Nov 27 10:08:59 2019 +0800


    去掉protobuf依赖


diff --git a/build.gradle b/build.gradle
index 0a39454..8f91621 100644
--- a/build.gradle
+++ b/build.gradle
@@ -7,8 +7,8 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.0.1'
-        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.2'
+        classpath 'com.android.tools.build:gradle:3.5.2'
+//        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
         // protobuf插件,AS3.0需要protobuf版本在0.8.2及以上


         //compile 'com.android.support:appcompat-v7:26.0.0'


commit f3f13da57e62c63b374de48bd991173b28165fc2
Author: songzeceng <songzeceng@360buyad.local>
Date:   Fri Feb 15 14:04:45 2019 +0800


    protobuf的使用


diff --git a/build.gradle b/build.gradle
index 02a3812..0a39454 100644
--- a/build.gradle
+++ b/build.gradle
@@ -8,6 +8,9 @@ buildscript {
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.0.1'
+        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.2'
+        // protobuf插件,AS3.0需要protobuf版本在0.8.2及以上
+
         //compile 'com.android.support:appcompat-v7:26.0.0'
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files


commit 0a4138cb58576c84cc266b25c239c71b9cd1ab7f
Author: Song zeceng <songzeceng@bytedance.com>
Date:   Sun Feb 11 11:10:21 2018 +0800


    2018/02/11


diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..02a3812
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,29 @@
+// Top-level build file where you can add configuration options common to all sub-projects/modules.
+
+buildscript {
+
+    repositories {
+        google()
+        jcenter()
+    }
+    dependencies {
+        classpath 'com.android.tools.build:gradle:3.0.1'
+        //compile 'com.android.support:appcompat-v7:26.0.0'
+        // NOTE: Do not place your application dependencies here; they belong
+        // in the individual module build.gradle files
+    }
+}
+
+allprojects {
+    repositories {
+        google()
+        jcenter()
+        maven {
+            url "https://maven.google.com"
+        }
+    }
+}
+
+task clean(type: Delete) {
+    delete rootProject.buildDir
+}

输出内容解析

从上往下是从新到旧的每个提交信息,每一个提交信息的格式都是

commit commitID
Author: 作者名 <作者邮箱>
Date:   提交时间,星期 月 日 时:分:秒 年 时区


    提交信息


diff --git a/提交前当前文件相对路径 b/提交后当前文件相对路径
提交索引
对源文件1的修改信息

举个例子:

commit f524eeda210b6d2e92dcd43dc13f1831d5bcf94c
Author: songzeceng <392004248@qq.com>
Date:   Thu Dec 12 21:08:09 2019 +0800


    KeyStore加解密


diff --git a/build.gradle b/build.gradle
index 8f91621..20ea27a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -7,7 +7,7 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:3.5.2'
+        classpath 'com.android.tools.build:gradle:3.5.3'
//        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
         // protobuf插件,AS3.0需要protobuf版本在0.8.2及以上

以上提交信息表示我在19年12月12号晚上9:08发起了一个提交,build/gradle的提交内容就是修改了下com.android.tools.build:gradle的版本(3.5.2->3.5.3)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值