• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • git diff

    git diff:此命令描述已临时提交的或者已修改但尚未提交的改动。执行git diff比较历史记录中的两个不同的点,通常是为了看看两个分支有啥区别,或者从某个版本到另一个版本,你的软件都有啥变化。

    git diff

    git diff:尚未缓存的改动。如果没有其他参数,git diff会以规范化的 diff 格式(一个补丁)显示自从你上次提交快照之后尚未缓存的所有更改。

    vim hello.rb
    git status -s
     M hello.rb
    
    git diff
    diff --git a/hello.rb b/hello.rb
    index d62ac43..8d15d50 100644
    --- a/hello.rb
    +++ b/hello.rb
    @@ -1,7 +1,7 @@
     class HelloWorld
    
       def self.hello
    -    puts "hello world"
    +    puts "hola mundo"
       end
    
     end
    

    所以,git status显示你上次提交更新至后所更改或者写入缓存的改动,而git diff一行一行地显示这些改动具体是啥。通常执行完git status之后接着跑一下git diff是个好习惯。


    git diff --cached

    git diff --cached:查看已缓存的改动。git diff --cached命令会告诉你有哪些内容已经写入缓存了。也就是说,此命令显示的是接下来要写入快照的内容。所以,如果你将上述示例中的hello.rb写入缓存,因为git diff显示的是尚未缓存的改动,所以在此执行它不会显示任何信息。

    git status -s
     M hello.rb
    
    git add hello.rb 
    git status -s
    M  hello.rb
    
    git diff
    

    如果你想看看已缓存的改动,你需要执行的是git diff --cached

    git status -s
    M  hello.rb
    
    git diff
    git diff --cached
    diff --git a/hello.rb b/hello.rb
    index d62ac43..8d15d50 100644
    --- a/hello.rb
    +++ b/hello.rb
    @@ -1,7 +1,7 @@
     class HelloWorld
    
       def self.hello
    -    puts "hello world"
    +    puts "hola mundo"
       end
    
     end
    


    git diff HEAD

    git diff HEAD:查看已缓存的与未缓存的所有改动。如果你想一并查看已缓存的与未缓存的改动,可以执行git diff HEAD——也就是说你要看到的是工作目录与上一次提交的更新的区别,无视缓存。假设我们又改了些ruby.rb的内容,那缓存的与未缓存的改动我们就都有了。以上三个diff命令的结果如下:

    vim hello.rb 
    git diff
    diff --git a/hello.rb b/hello.rb
    index 4f40006..2ae9ba4 100644
    --- a/hello.rb
    +++ b/hello.rb
    @@ -1,7 +1,7 @@
     class HelloWorld
    
    +  # says hello
       def self.hello
         puts "hola mundo"
       end
    
     end
    
    git diff --cached
    diff --git a/hello.rb b/hello.rb
    index 2aabb6e..4f40006 100644
    --- a/hello.rb
    +++ b/hello.rb
    @@ -1,7 +1,7 @@
     class HelloWorld
    
       def self.hello
    -    puts "hello world"
    +    puts "hola mundo"
       end
    
     end
    
    git diff HEAD
    diff --git a/hello.rb b/hello.rb
    index 2aabb6e..2ae9ba4 100644
    --- a/hello.rb
    +++ b/hello.rb
    @@ -1,7 +1,8 @@
     class HelloWorld
    
    +  # says hello
       def self.hello
    -    puts "hello world"
    +    puts "hola mundo"
       end
    
     end
    


    git diff --stat

    git diff --stat:显示摘要而非整个 diff。如果我们不想要看整个 diff 输出,但是又想比git status详细点,就可以用--stat选项。该选项使它显示摘要而非全文。上文示例在使用--stat选项时,输出如下:

    git status -s
    MM hello.rb
    
    git diff --stat
     hello.rb |    1 +
     1 files changed, 1 insertions(+), 0 deletions(-)
    
    git diff --cached --stat
     hello.rb |    2 +-
     1 files changed, 1 insertions(+), 1 deletions(-)
    
    git diff HEAD --stat
     hello.rb |    3 ++-
     1 files changed, 2 insertions(+), 1 deletions(-)
    

    你还可以在上述命令后面制定一个目录,从而只查看特定文件或子目录的diff输出。

    简而言之,执行git diff来查看执行git status的结果的详细信息——一行一行地显示这些文件是如何被修改或写入缓存的。

    上篇:git status

    下篇:git rm