Previous commit ~
#
You can use the <commit>~n
construction to reference a commit that is n
steps back in the commit chain.
Basic example#
In the following example:
Two commits were created;
git log
shows thatHEAD
refer to second commit;git show HEAD~1
show the idea:git show
is a command that displays information about a passed commit;HEAD~1
is passed as an argument togit show
, which means that a commit back from commitHEAD
refers to;So we got information about
first commit
, which is correct, it is a commit back fromsecond commit
.
%%bash
mkdir operations_examples
cd operations_examples
git init &> /dev/null
touch file1
git add --all
git commit -am "first commit" &> /dev/null
touch file2
git add --all
git commit -am "second commit" &> /dev/null
echo "=====git log====="
git log --oneline --decorate
echo "=====git show====="
git show HEAD~1
cd ..
rm -r operations_examples
=====git log=====
e94d28e (HEAD -> master) second commit
73e5ca8 first commit
=====git show=====
commit 73e5ca80b04925b1f03d6b9e0b164a796439ff71
Author: Fedor Kobak <kobfedsur@gmail.com>
Date: Sun Sep 10 10:25:44 2023 +0300
first commit
diff --git a/file1 b/file1
new file mode 100644
index 0000000..e69de29
Use hash#
I was wondering if it is possible to use the commit hash in this construct, i.e. <hash>~1
? Yes, you can! The following example confirms it:
Created two commits,
first commit
andsecond commit
;Stored hash of
second commit
in variableI used variable with syntax
~1
;All is well - I got info on
first commit
, which is one beforesecond commit
.
%%bash
mkdir operations_examples
cd operations_examples
git init &> /dev/null
touch file1
git add --all
git commit -am "first commit" &> /dev/null
touch file2
git add --all
git commit -am "second commit" &> /dev/null
second_hash=$(git rev-parse HEAD)
echo "=====git log====="
git log --oneline --decorate
echo "=====git show====="
git show $second_hash~1
cd ..
rm -r operations_examples
=====git log=====
e79a082 (HEAD -> master) second commit
3d2a63f first commit
=====git show=====
commit 3d2a63fcbd20eb9730ae250cef9ab6133e60ef19
Author: Fedor Kobak <kobfedsur@gmail.com>
Date: Sun Sep 10 10:35:54 2023 +0300
first commit
diff --git a/file1 b/file1
new file mode 100644
index 0000000..e69de29