git diff

git diff命令用于查看文件之间的差异。

  1. # 查看工作区与暂存区的差异
  2. $ git diff
  3. # 查看某个文件的工作区与暂存区的差异
  4. $ git diff file.txt
  5. # 查看暂存区与当前 commit 的差异
  6. $ git diff --cached
  7. # 查看两个commit的差异
  8. $ git diff <commitBefore> <commitAfter>
  9. # 查看暂存区与仓库区的差异
  10. $ git diff --cached
  11. # 查看工作区与上一次commit之间的差异
  12. # 即如果执行 git commit -a,将提交的文件
  13. $ git diff HEAD
  14. # 查看工作区与某个 commit 的差异
  15. $ git diff <commit>
  16. # 显示两次提交之间的差异
  17. $ git diff [first-branch]...[second-branch]
  18. # 查看工作区与当前分支上一次提交的差异,但是局限于test文件
  19. $ git diff HEAD -- ./test
  20. # 查看当前分支上一次提交与上上一次提交之间的差异
  21. $ git diff HEAD -- ./test
  22. # 生成patch
  23. $ git format-patch master --stdout > mypatch.patch

比较两个分支

  1. # 查看topic分支与master分支最新提交之间的差异
  2. $ git diff topic master
  3. # 与上一条命令相同
  4. $ git diff topic..master
  5. # 查看自从topic分支建立以后,master分支发生的变化
  6. $ git diff topic...master