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
@@ -, +, @@ 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.
file changed, insertions(+), 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 输出均无内容。

上一篇:<>读书笔记


下一篇:Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator