zoukankan      html  css  js  c++  java
  • git diff 命令用法

    理解git diff的前提,首先要理解git中工作区,暂存区,本地版本库的概念,如果头脑中有这些概念,接着往下读。

    git diff test.c 用来查看工作区和暂存区中test.c文件的区别。

    git diff HEAD -- test.c 用来查看工作区和本地版本库中test.c文件的区别。

    eg:

    a. 在工作区中修改文件cfm_test.c,git diff cfm_test.c 和 git diff HEAD --  cfm_test.c 分别输出如下:

    [3me]$ git diff cfm_test.c
    @@ -45,7 +45,11 @@ cfm_test_init ()
       tst->tst_data = XCALLOC (MTYPE_ITUT_CFM_MSG, CFM_TST_PDU_MAX_LENGTH);
       if (!tst->tst_data)
         {
    -      XFREE (MTYPE_ITUT_CFM_MSG, tst->tst_data);
    +#if defined(CONFIG_DS_COVERITY)
    +	XFREE( MTYPE_ITUT_CFM_TST, tst);
    +#else
    +	XFREE (MTYPE_ITUT_CFM_MSG, tst->tst_data);
    +#endif
           return NULL;
         }
     
    [3me]$ git diff HEAD -- cfm_test.c
    @@ -45,7 +45,11 @@ cfm_test_init ()
       tst->tst_data = XCALLOC (MTYPE_ITUT_CFM_MSG, CFM_TST_PDU_MAX_LENGTH);
       if (!tst->tst_data)
         {
    -      XFREE (MTYPE_ITUT_CFM_MSG, tst->tst_data);
    +#if defined(CONFIG_DS_COVERITY)
    +	XFREE( MTYPE_ITUT_CFM_TST, tst);
    +#else
    +	XFREE (MTYPE_ITUT_CFM_MSG, tst->tst_data);
    +#endif
           return NULL;
         }
     
    

    b. 执行git add cfm_test.c,将文件提交到暂存区,输出如下:

    [3me]$ git add cfm_test.c
    [3me]$ git diff cfm_test.c
    [3me]$ git diff HEAD -- cfm_test.c
    @@ -45,7 +45,11 @@ cfm_test_init ()
       tst->tst_data = XCALLOC (MTYPE_ITUT_CFM_MSG, CFM_TST_PDU_MAX_LENGTH);
       if (!tst->tst_data)
         {
    -      XFREE (MTYPE_ITUT_CFM_MSG, tst->tst_data);
    +#if defined(CONFIG_DS_COVERITY)
    +    XFREE( MTYPE_ITUT_CFM_TST, tst);
    +#else
    +    XFREE (MTYPE_ITUT_CFM_MSG, tst->tst_data);
    +#endif
           return NULL;
         }
     

    分析:由于已经将文件提交到暂存区,所以工作区和暂存区中文件无区别,所以 git diff cfm_test.c 无输出内容;由于未提交到本地版本库,工作区和本地版本库文件有区别,所以 git diff HEAD -- cfm_test.c 输出diff内容。

    c. 接着执行git commit 将文件提交到本地版本库,输出如下:

    [3me]$ git commit -m "for source code coverity check."
    [master 444ebb0f84] for source code coverity check.
     1 file changed, 5 insertions(+), 1 deletion(-)
    [3me]$ git diff cfm_test.c
    error: cannot run most: No such file or directory
    [3me]$ git diff HEAD -- cfm_test.c
    error: cannot run most: No such file or directory

    分析:执行 commit 将文件提交到本地版本库, 因此工作区,暂存区,本地库三者之间的内容是一致的,因此git diff 和git diff HEAD 输出均无内容。

  • 相关阅读:
    LuoguP2763 试题库问题(最大流)
    LuoguP3254 圆桌问题(最大流)
    LuoguP2765 魔术球问题(最大流)
    LuoguP2764 最小路径覆盖问题(最大流)
    LuoguP4016 负载平衡问题(费用流)
    LuoguP2756 飞行员配对方案问题(最大流)
    BZOJ3675: [Apio2014]序列分割(斜率优化Dp)
    BZOJ1814: Ural 1519 Formula 1(插头Dp)
    BZOJ4652: [Noi2016]循环之美(莫比乌斯反演,杜教筛)
    BZOJ4916: 神犇和蒟蒻(杜教筛)
  • 原文地址:https://www.cnblogs.com/3me-linux/p/6639692.html
Copyright © 2011-2022 走看看