---
title: "（转载）git diff命令详解"
author: "forestqqqq"
date: "2016-02-17T15:18:19Z"
tags: ["Git", "git diff"]
categories: ["开源软件"]
slug: "git-diff"
draft: false
lastmod: "2016-02-17T15:18:19Z"
id: 2276969
url: "https://forestqqqq.iteye.com/blog/2276969"
---

原文：http://www.cnblogs.com/wish123/p/3963224.html

 

 

a、查看尚未暂存的文件更新了哪些部分，不加参数直接输入  
    git diff  
此命令比较的是工作目录(Working tree)和暂存区域快照(index)之间的差异  
也就是修改之后还没有暂存起来的变化内容。

b、查看已经暂存起来的文件(staged)和上次提交时的快照之间(HEAD)的差异  
    git diff --cached  
    git diff --staged  
显示的是下一次commit时会提交到HEAD的内容(不带-a情况下)

c、显示工作版本(Working tree)和HEAD的差别  
    git diff HEAD

d、直接将两个分支上最新的提交做diff  
    git diff topic master 或 git diff topic..master

e、输出自topic和master分别开发以来，master分支上的changed。  
    git diff topic...master  
Changes that occurred on the master branch since when the topic  
 branch was started off it  
f、查看简单的diff结果，可以加上--stat参数  
    git diff --stat

g、查看当前目录和另外一个分支的差别  
    git diff test  
显示当前目录和另一个叫'test'分支的差别  
    git diff HEAD -- ./lib  
显示当前目录下的lib目录和上次提交之间的差别（更准确的说是在当前分支下）

h、比较上次提交commit和上上次提交  
    git diff HEAD^ HEAD

i、比较两个历史版本之间的差异  
    git diff SHA1 SHA2