git log疑点剖析

Published: 16 Aug 2013 Category: git

使用过git的人肯定知道git log这个命令,他们也知道这个命令的功能是什么。简单来说,就是显示历史提交(commit)记录。他们也知道这个命令的一些其他用法,例如:

git log --pretty=oneline

等等一些其他很强大的功能,这里也不多说。你可以去just google it。但是,我觉得很少人真正分析过git log。今天,我就想简单分析一下git log,在这里我就抛砖引玉了。

这里,我们主要讨论两种情况:

  1. 当前分支是develop分支,历史提交(commit)是干净的,没有其他分支合并到当前分支上来
  2. 当前分支是master分支,存在自己的提交(commit),也有其他分支(develop)合并到当前分支上来

先讨论第一种情况:

git checkout develop
git log --pretty=oneline

看一下终端消息,我们只截取最新提交的一部分:

927b4ac8336c0fa0701ae42dfb59b1023657c41d add delete use lock file when factory set
fd0d7d78f69bbea0b1efb7f0090a817d77191183 V00.00.00.00.06
99afd38622f78d17319aef209a7b6a5f1d5f2aa4 modified capture speed value in oa_area.c
94ec549f956c17f1d72a0c5615d729125337cf8f modified default params
b90edda93c3c8f25ec1a05653036973b42678f68 delete cancel overspeed in oa_area.c
c378abb1c7cd6b1cc806c0d7a693d872077aa61c don't delete use lock file when do factory settings
787205d4d28aaf2f4e1822711848c78c7cd245d8 clear rubbish data when socket send err
077ec69d6b1028f3d66a5af9203af3b9b82cbc42 delete oa_sram_noinit_reset() when send err
db577b783044ceb738f9d7f46ae2d38a7743a5df delete some __FILE__ in src/oa_soc.c
a5b3976acfd00962c81154ca6e6ef1ce5a96f667 factory setting don't change devid
015c871b1441c065a6a35d8ab4c7ddb7bba3c7fc modified README.md
3e617379a543a9b7562c321affe2a71ae2c7a351 fix a bug in oa_jt_808.c:set_area
72f7b36924a9372e9acc79945a095d717e987625 add g_soc_context.is_blocksend == OA_TRUE in oa_blinddata.c
358eab8bb27db668ecc82b15312c45a3af3d161e modifie some code in src/oa_gps.c  

这种情况最简单不过了,所有提交都是自己分支上的提交,这没什么值得讨论的。

我们再看第二种情况:

git checkout master
git log --pretty=oneline

1168826c4206b4ea38657e837f3e92c36bf588cd add delete use lock file when set factory
927b4ac8336c0fa0701ae42dfb59b1023657c41d add delete use lock file when factory set
fd0d7d78f69bbea0b1efb7f0090a817d77191183 V00.00.00.00.06
fe5a7ebc79051c003109a1df2977f881bc4cf148 V00.00.00.00.06
16b31813ababcf963a839acf41aed8a5456e53a3 Merge branch 'temp' into t100g-ublox
99afd38622f78d17319aef209a7b6a5f1d5f2aa4 modified capture speed value in oa_area.c
94ec549f956c17f1d72a0c5615d729125337cf8f modified default params
b90edda93c3c8f25ec1a05653036973b42678f68 delete cancel overspeed in oa_area.c
c378abb1c7cd6b1cc806c0d7a693d872077aa61c don't delete use lock file when do factory settings
787205d4d28aaf2f4e1822711848c78c7cd245d8 clear rubbish data when socket send err
077ec69d6b1028f3d66a5af9203af3b9b82cbc42 delete oa_sram_noinit_reset() when send err
db577b783044ceb738f9d7f46ae2d38a7743a5df delete some __FILE__ in src/oa_soc.c
224ab946a6ea8a14e04a53c1015fdb7156d38190 Merge branch 'temp' into t100g-ublox
a5b3976acfd00962c81154ca6e6ef1ce5a96f667 factory setting don't change devid
1251080c418ae3a6705133832441b57b4652acbf Merge branch 'temp' into t100g-ublox
015c871b1441c065a6a35d8ab4c7ddb7bba3c7fc modified README.md  

仔细看提交记录,你会发现其中某些提交(commit)和第一种情况中的一样,例如:

927b4ac8336c0fa0701ae42dfb59b1023657c41d add delete use lock file when factory set  
fd0d7d78f69bbea0b1efb7f0090a817d77191183 V00.00.00.00.06  

等等,没错,这些相同的提交(commit)都是从develop上合并到当前分支(master)上来的,这里我想说的是:

  1. 如果当前分支是干净的分支,历史提交是线性的,没有其他分支合并到当前分支上来,git log 只会显示本分支上的历史提交
  2. 如果当前分支存在自己的提交,也有其他分支合并到当前分支上来,git log不但会显示本分支上的历史提交还会显示合并到当前分支上,其他分支上的提交