diff命令都是以行作为比较单位的,如果在某行添加了信息,文件差异比较相当于删除了这行,然后又在此处重新添加了一行。
比较文件差异:
git diff [文件名]
将工作区中的文件和暂存区进行比较
git diff [本地库中历史版本] [文件名] 比如: git diff head bbb.txt
将工作区中的文件和本地库历史记录比较
不带文件名可以比较多个文件
一、添加了两个新文件并提交到本地库
cooldeMacBook-Pro:git-learn cool$ git commit a.txt bbb.txt -m "两个新文件" [master ee9a560] 两个新文件 2 files changed, 11 insertions(+) create mode 100644 a.txt create mode 100644 bbb.txt cooldeMacBook-Pro:git-learn cool$ git status On branch master nothing to commit, working tree clean cooldeMacBook-Pro:git-learn cool$
接着工作区修改了a.txt ,但是还没有添加到暂存区
cooldeMacBook-Pro:git-learn cool$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: a.txt no changes added to commit (use "git add" and/or "git commit -a") cooldeMacBook-Pro:git-learn cool$
二、工作区与暂存区比较
采用了diff 命令:
cooldeMacBook-Pro:git-learn cool$ git diff a.txt diff --git a/a.txt b/a.txt index 07b3c9e..f9bbd73 100644 --- a/a.txt +++ b/a.txt @@ -1,6 +1,6 @@ sd asdsad -dfsdf +dfsdf ##这一行末尾添加了信息 asd soft commit cooldeMacBook-Pro:git-learn cool$
三、工作区与本地库比较
当前工作区 修改了 a.txt ,没有修改 bbb.txt
1、比较a.txt
cooldeMacBook-Pro:git-learn cool$ git diff head a.txt diff --git a/a.txt b/a.txt index 07b3c9e..f9bbd73 100644 --- a/a.txt +++ b/a.txt @@ -1,6 +1,6 @@ sd asdsad -dfsdf +dfsdf ##这一行末尾添加了信息 asd soft commit ============上面是工作区与当前游标本地库比较=================== ============下面是工作区与上一个游标本地库比较=================== cooldeMacBook-Pro:git-learn cool$ git diff head^ a.txt diff --git a/a.txt b/a.txt new file mode 100644 index 0000000..f9bbd73 --- /dev/null +++ b/a.txt @@ -0,0 +1,7 @@ +sd +asdsad +dfsdf ##这一行末尾添加了信息 + +asd +soft commit + cooldeMacBook-Pro:git-learn cool$
2、比较b.txt
cooldeMacBook-Pro:git-learn cool$ git diff head bbb.txt cooldeMacBook-Pro:git-learn cool$ #### git diff head bbb.txt 当前本地仓库的当前游标下的 bbb.txt 与工作区的 bbb.txt 文件差异比较 ========================== cooldeMacBook-Pro:git-learn cool$ git diff head~1 bbb.txt diff --git a/bbb.txt b/bbb.txt new file mode 100644 index 0000000..221c50e --- /dev/null +++ b/bbb.txt @@ -0,0 +1,4 @@ +bob +sd +主线程 +主线程 cooldeMacBook-Pro:git-learn cool$ ========================== git diff head~1 bbb.txt 代表当前游标的上一个版本的 bbb.txt 与工作区的bbb.txt 差异比较