linux中diff命令的使用詳解

來源:互聯網
上載者:User

diff命令是版本管理工具的基礎,本文主要講解diff比較幾種方式和git diff提示。
如下兩個檔案,file1和file2

$ cat file1
aa
aa
aa
aa
aa
$ cat file2
aa
aa
bb
aa
aa

1.普通格式diff file1 file2
$ diff file1 file2
3c3
< aa
---
> bb

說明:3c3表示file1的第3行有變化(change),變動為file2的第3行;<表示從file1當中去除該行;---分割file1和file2;>表示file2增加了該行
2.上下文格式diff -c file1 file2
$ diff -c file1 file2
*** file1 2016-04-04 15:56:25.203923276 +0800
--- file2 2016-04-04 15:56:47.647138613 +0800
***************
*** 1,5 ****
  aa
  aa
! aa
  aa
  aa
--- 1,5 ----
  aa
  aa
! bb
  aa
  aa

說明:
(1)前兩行表示比較檔案的基本資料檔案名稱和時間等。"***"表示變動前的檔案,"---"表示變動後的檔案。
*** file1 2016-04-04 15:56:25.203923276 +0800
--- file2 2016-04-04 15:56:47.647138613 +0800

(2)***************表示將基本資料和變動資訊分割開
(3)*** 1,5 ****表示從第1行開始,聯絡5行
(4)如果是驚嘆號(!),表示該行有改動;如果是減號(-),表示該行被刪除;如果是加號(+),表示該行為新增。
3.合并模式diff -u file1 file2
為瞭解決大量內容重複顯示而引入,git就是使用衍生的合并模式
$ diff -u file1 file2
--- file1 2016-04-04 15:56:25.203923276 +0800
+++ file2 2016-04-04 15:56:47.647138613 +0800
@@ -1,5 +1,5 @@
 aa
 aa
-aa
+bb
 aa
 aa

說明:
(1)表示檔案基本資料,"---"表示變動前的檔案,"+++"表示變動後的檔案。
--- file1 2016-04-04 15:56:25.203923276 +0800
+++ file2 2016-04-04 15:56:47.647138613 +0800

(2) 變動位置首末用兩個@表示,"-1,5"表示第一個檔案從第1行開始的連續5行,"+1,5"表示第二個檔案從第1行開始的連續5行
@@ -1,5 +1,5 @@

(3)變動具體內容,減號表示第一個檔案刪除的行,加號表示第二個檔案新增的.
看看版本管理工具git diff效果:
diff --git a/file1 b/file1
index a57a69a..863575a 100644
--- a/file1
+++ b/file1
@@ -1,5 +1,5 @@
 aa
 aa
-aa
+bb
 aa
 aa

git diff常用比較方法
git diff #工作區和暫存區比較
git diff master #工作區和master分支比較
git diff HEAD #工作區和HEAD比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.