It is also useful to be able to see what changes were included in a specific commit.
When you examine a commit, normally what you see is the 'changes since the previous commit'. This should not be interpreted as Git commits contain only the changes. As you recall, a Git commit contains a full snapshot of the working directory. However, tools used to examine commits show only the changes, as that is the more informative part.
Git shows changes included in a commit by dynamically calculating the difference between the snapshots stored in the target commit and the parent commit. This is because Git commits stores snapshots of the working directory, not changes themselves.
To address a specific commit, you can use its SHA (e.g., e60deaeb2964bf2ebc907b7416efc890c9d4914b
). In fact, just the first few characters of the SHA is enough to uniquely address a commit (e.g., e60deae
).
Naturally, a commit can be addressed using any ref pointing to it too (e.g., HEAD
, master
).
Another related technique is to use the <ref>~<n>
notation (e.g., HEAD~1
) to address the commit that is n
commits prior to the commit pointed by <ref>
i.e., "start with the commit pointed by <ref>
and go back n
commits".
A shortcut of this notation is to use HEAD~
, HEAD~~
, HEAD~~~
, ... to mean HEAD~1
, HEAD~2
, HEAD~3
etc.
HEAD
or master
HEAD~1
or master~1
HEAD~2
or master~2
Git uses the diff format to show file changes in a commit. The diff format was originally developed for Unix, later extended with headers and metadata to show changes between file versions and commits. Here is an example diff showing the changes to a file.
diff --git a/fruits.txt b/fruits.txt
index 7d0a594..f84d1c9 100644
--- a/fruits.txt
+++ b/fruits.txt
@@ -1,6 +1,6 @@
-apples
+apples, apricots
bananas
cherries
dragon fruits
-elderberries
figs
@@ -20,2 +20,3 @@
oranges
+pears
raisins
diff --git a/colours.txt b/colours.txt
new file mode 100644
index 0000000..55c8449
--- /dev/null
+++ b/colours.txt
@@ -0,0 +1 @@
+a file for colours
A Git diff can consist of multiple file diffs, one for each changed file. Each file diff can contain one or more hunk i.e., a localised group of changes within the file — including lines added, removed, or left unchanged (included for context).
Here is the explanation of the above diff:
file diff for fruits.txt
:
diff --git a/fruits.txt b/fruits.txt
index 7d0a594..f84d1c9 100644
--- a/fruits.txt
+++ b/fruits.txt
Hunk 1:
@@ -1,6 +1,6 @@
-apples
+apples, apricots
bananas
cherries
dragon fruits
-elderberries
figs
Hunk 2:
@@ -20,2 +20,3 @@
oranges
+pears
raisins
file diff for colours.txt
:
diff --git a/colours.txt b/colours.txt
new file mode 100644
index 0000000..55c8449
--- /dev/null
+++ b/colours.txt
Hunk 1:
@@ -0,0 +1 @@
+a file for colours
Here is an explanation of the diff:
Part of Diff | Explanation |
---|---|
diff --git a/fruits.txt b/fruits.txt | The diff header, indicating that it is comparing the file fruits.txt between two versions: the old (a/ ) and new (b/ ). |
index 7d0a594..f84d1c9 100644 | Shows the SHA of the file (not the commit) before and after the change, and the file mode (100 means a regular file, 644 are file permission indicators). |
--- a/fruits.txt +++ b/fruits.txt | Marks the old version of the file (a/fruits.txt ) and the new version of the file (b/fruits.txt ). |
| This hunk header shows that lines 1-6 (i.e., starting at line 1 , showing 6 lines) in the old file were compared with lines 1–6 in the new file. |
-apples +apples, apricots | Removed line apples and added line apples, apricots . |
bananas cherries dragon fruits | Unchanged lines, shown for context. |
-elderberries | Removed line: elderberries . |
figs | Unchanged line, shown for context. |
| Hunk header showing that lines 20-21 in the old file were compared with lines 20–22 in the new file. |
oranges +pears raisins | Unchanged line. Added line: pears .Unchanged line. |
diff --git a/colours.txt b/colours.txt | The usual diff header, indicates that Git is comparing two versions of the file colours.txt : one before and one after the change. |
new file mode 100644 | This is a new file being added. 100644 means it’s a normal, non-executable file with standard read/write permissions. |
index 0000000..55c8449 | The usual SHA hashes for the two versions of the file. 0000000 indicates the file did not exist before. |
--- /dev/null +++ b/colours.txt | Refers to the "old" version of the file (/dev/null means it didn’t exist before), and the new version. |
@@ -0,0 +1 @@ | Hunk header, saying: “0 lines in the old file were replaced with 1 line in the new file, starting at line 1.” |
+a file for colours | Added line |
Points to note:
+
indicates a line being added.
-
indicates a line being deleted.- Editing a line is seen as deleting the original line and adding the new line.
View contents of specific commits in a repo (e.g., the things
repo):
1 Locate the commits to view, using the revision graph.
git log --oneline --decorate
e60deae (HEAD -> master, origin/master) Update fruits list
f761ea6 Add colours.txt, shapes.txt
2bedace Add figs to fruits.txt
d5f91de Add fruits.txt
2 Use the git show
command to view specific commits.
git show # shows the latest commit
commit e60deaeb2964bf2ebc907b7416efc890c9d4914b (HEAD -> master, origin/master)
Author: damithc <...@...>
Date: Sat Jun ...
Update fruits list
diff --git a/fruits.txt b/fruits.txt
index 7d0a594..6d502c3 100644
--- a/fruits.txt
+++ b/fruits.txt
@@ -1,6 +1,6 @@
-apples
+apples, apricots
bananas
+blueberries
cherries
dragon fruits
-elderberries
figs
To view the parent commit of the latest commit, you can use any of these commands:
git show HEAD~1
git show master~1
git show e60deae # first few characters of the SHA
git show e60deae..... # run git log to find the full SHA and specify the full SHA
to view the one two commits prior to the latest commit, you can use git show HEAD~2
etc.
Click on the commit. The remaining panels (indicated in the image below) will be populated with the details of the commit.
