git diff详细使用

git diff

使用–stat参数表示仅显示统计信息, 而不显示具体的差异

  • git diff [<options>] --no-index [--] <path> <path>

    显示文件系统上两个文件的差异, 当两者至少一个文件在git管理下, 或者在git 仓库中运行该命令, 你可以省略–no-index参数

  • git diff [<options>] [--] [<path>…] 显示工作区和暂存区的差异

  • git diff [<options>] --cached/–staged [<commit>] [--] [<path>...]

    显示暂存区和提交版本的差异, commit默认为HEAD, --cached和–staged同义

    # 显示暂存区中readme.md和上次提交的差异
    git diff --cached HEAD readme.md
    git diff --cached readme.md
    git diff --staged readme.md
    
    # 显示暂存区中readme.md和上上次提交的差异
    git diff --cachd HEAD^^ readme.md
    
  • git diff [<options>] <commit> [--] [<path>...]`

    • 显示工作区与提交版本的差异

      # 显示工作区与上次提交的差异, 也就是显示待提交的内容
      git diff HEAD
      
      # 显示工作区中readme.md与上次提交时的差异
      git diff HEAD readme.md
      
      # 显示工作区中readme.md与上上次提交时的差异
      git diff HEAD^ readme.md
      
    • 也可以表示分支名, 此时显示的是当前分支工作区与分支最近一次提交的差异

      # 显示工作区中readme.md与test分支最近一次提交的差异
      # 该命令不加--的话是有歧义的, 有可能你是想比较test和readme.md的差异
      git diff test -- readme.md
            
      # 显示工作区与test分支最近一次提交
        
      # 该命令不加--是有歧义的, 有可能你是想比较工作区的test和暂存区中的test文件
      git diff test -- 
      
  • git diff [<options>] <commit> <commit> [--] [<path>...]

    git diff [<options>] <commit>..<commit> [--] [<path>...]

    • 当commit为提交版本时, 比较两个版本差异

      # 把上次提交的readme.md和上上次提交的readme.md相比较
      git diff HEAD HEAD^ readme.md
      
      # 把上次提交和上上次提交相比较
      git diff HEAD HEAD^ readme.md
      

      需要注意的是git diff HEAD HEAD^和git diff HEAD^ HEAD是不同的结果

      当我们在readme.md中添加一行 11111并且提交
      git diff HEAD HEAD^ readme.md 显示 +11111
      git diff HEAD^ HEAD readme.md 显示 -11111
      
    • 当commit为分支名时, 比较两个分支的差异,

      # 把test分支和dev分支相比较
      # --不能省略, 否则有歧义
      git diff test dev --
      
      #把test分支的readme.md和dev分支的readme.md相比较
      git diff test dev -- readme.md
      

      需要注意的是git diff test dev – 和git diff dev test --是不同结果的

  • git diff [<options>] <commit1>...<commit2> [--] [<path>...]

    查看输出自commit1和commit2分别开发以来,commit2分支上的变更。两者必须有共同的祖先

    # 现有分支dev, 内有一个readme.md
    git checkout -b test # 创建一个test branch, 现在两个分支readme.md一样
    
    # 在dev分支上的readme.md添加一行11111并提交, test分支上的readme.md不修改
    git diff test...dev readme.md  # 显示+11111
    git diff dev...test readme.md  # 显示没有差异, 因为test和dev分来以后, test分支上的readme.md没有被修改
    

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页