1.作用:
git diff用来显示我们对文件做了何种更改
注意:git diff查看的是还没有暂存起来的改动,而不是这次工作和上次提交之间的差异(因为可能你已经暂存过,之后又对文件进行了更改,diff显示的是这些改动)
git diff --cached 是查看已经暂存起来的变化
2.用法:
首先我们可以执行git status查看修改过的文件,可以看到有两个文件被修改
git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: src/thinkinjava/chapter20_annotations/UseCase.java
modified: src/thinkinjava/chapter20_annotations/UseCaseTracker.java
Untracked files:
(use "git add <file>..." to include in what will be committed)
OSS/
PRD/
test/
no changes added to commit (use "git add" and/or "git commit -a")
执行git diff可以查看所有的未暂存的修改
git diff
warning: LF will be replaced by CRLF in src/thinkinjava/chapter20_annotations/UseCase.java.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in src/thinkinjava/chapter20_annotations/UseCaseTracker.java.
The file will have its original line endings in your working directory
diff --git a/src/thinkinjava/chapter20_annotations/UseCase.java b/src/thinkinjava/chapter20_annotations/UseCase.java
index 672c5da..82475d3 100644
--- a/src/thinkinjava/chapter20_annotations/UseCase.java
+++ b/src/thinkinjava/chapter20_annotations/UseCase.java
@@ -15,5 +15,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface UseCase {
public int id();
- public String description() default "no description";
+ public String description() default "no descriptions";
}
diff --git a/src/thinkinjava/chapter20_annotations/UseCaseTracker.java b/src/thinkinjava/chapter20_annotations/UseCaseTracker.java
index ce0e229..750ad74 100644
--- a/src/thinkinjava/chapter20_annotations/UseCaseTracker.java
+++ b/src/thinkinjava/chapter20_annotations/UseCaseTracker.java
@@ -19,7 +19,7 @@ public class UseCaseTracker {
}
public static void main(String[] args) {
List<Integer> useCases = new ArrayList<Integer>();
- Collections.addAll(useCases, 47, 48, 49, 50);
+ Collections.addAll(useCases, 47, 48, 49);
trackUseCases(useCases, PasswordUtils.class);
}
} /* Output:
要是想查看某个文件的修改 可以执行git diff [<文件路径>]
git diff src/thinkinjava/chapter20_annotations/UseCase.java
warning: LF will be replaced by CRLF in src/thinkinjava/chapter20_annotations/UseCase.java.
The file will have its original line endings in your working directory
diff --git a/src/thinkinjava/chapter20_annotations/UseCase.java b/src/thinkinjava/chapter20_annotations/UseCase.java
index 672c5da..82475d3 100644
--- a/src/thinkinjava/chapter20_annotations/UseCase.java
+++ b/src/thinkinjava/chapter20_annotations/UseCase.java
@@ -15,5 +15,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface UseCase {
public int id();
- public String description() default "no description";
+ public String description() default "no descriptions";
}
如果已经通过git add命令将文件暂存,则需要通过git diff --cached查看
git diff --cached
diff --git a/src/thinkinjava/chapter20_annotations/UseCase.java b/src/thinkinjava/chapter20_annotations/UseCase.java
index 672c5da..82475d3 100644
--- a/src/thinkinjava/chapter20_annotations/UseCase.java
+++ b/src/thinkinjava/chapter20_annotations/UseCase.java
@@ -15,5 +15,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
public @interface UseCase {
public int id();
- public String description() default "no description";
+ public String description() default "no descriptions";
}
3.查看两个提交之间的差异
git diff a57d49d3fedefc6ddfb1 37d7375155819c170ffd7e