Git Diff
We can use the git diff command to view changes between commits, branches, files, our working directory, and more.
We often use git diff alongside commands like git status and git log, to get a better picture of a repository and how it has changed over time.
Without additional options, git diff lists all the changes in our working directory that are NOT staged for the next commit.
git diff => Compares Staging Area and Working Directory.
diff --git a/fileAddedByBranch1_01.txt b/fileAddedByBranch1_01.txt
index 832a740..d403b8a 100644
--- a/fileAddedByBranch1_01.txt
+++ b/fileAddedByBranch1_01.txt
@@ -1 +1,21 @@
+ ..
+ ..
- ..
Compared Files
diff --git a/fileAddedByBranch1_01.txt b/fileAddedByBranch1_01.txt
For each comparison, Git explains which files it is comparing. Usually this is two versions of the same file. Git also declares one file as "a" and the other as "b".
File Metadata
index 832a740..d403b8a 100644
we really do not need to care about the file metadata. The first two numbers are the hashes of the two files being compared. The last number is an internal file mode identifier.
Markers
--- a/fileAddedByBranch1_01.txt
+++ b/fileAddedByBranch1_01.txt
File A and File B are each assigned-
- File A gets a minus sign (-)
- File B gets a plus sign (+)
Chunks
@@ -1 +1,21 @@
+ ..
- ..
A diff won't show the entire contents of a file, but instead only shows portions or "chunks" that were modified. A chunk also includes some unchanged lines before and after a change to provide some context.
Chunck Header - "@@ -3,4 +3,5 @@" Each chunk starts with a chunk header, found between @@ and @@. From file a, 4 lines are extracted starting from line 3. From file b, 5 lines are extracted starting from line 3.
Every line that changed between the two files is marked with either a + or - symbol.
lines that begin with - come from file A
lines that begin with + come from file B
Other diff Options:
git diff --staged or --cached => will list the changes between the staging area and our last commit.
git diff HEAD [filename] => view the changes within a specific file by providing git diff with a filename.
git diff --staged [filename]
git diff branch1..branch2 => list the changes between the tips of branch1 and branch2
git diff commit1..commit2
git diff commit1..commit2 use to compare two commits, provide git diff with the commit hashes of the commits in question.