How to use Git Tools to review and analyse your commits
Here in this article we will try to see more on how we can review the git commits and try to details of the commits based on SHA1 hash or branch reference. We will also look at how we can view a range of commit ids and how to identify parents of a particular commit id. We will also look the git reference log which stores the completed activity locally and how we can filter it to identify any activity at a specific timeframe.
Test Environment
Fedora 37 workstation
Git v2.40.0
Here is my commit history for my project repository on which we will try to review, analyze and filter commit ids.
[admin@fedser learngit]$ git log --pretty=oneline
a4883be13a249e679b385b441e67ad3f33cc961f (HEAD -> edition1, origin/edition1) update .gitignore file
762f6a031fe6c19e3ead49d7693f9951288c4451 (origin/main, main) This is an update to same file with conflict resolved
1704b5c0ece8b6ba48106ec66cbcb5c20d6b462a This is an update to same file on edition branch
6a3ea2727dd91db77d37ff3bbdf1af5b2def8651 This is an update to same file on master branch
caa75f0783e83ddb724766d7f22c4c3147061687 created chapter4
23bb8bce61916a072a9fcaf8c64f2b3227c33252 created chapter4
5623dae5295eec03609df496e8969561b34f1ba7 created chapter4
f9c7919761b8a7be7e4a634d127b1b9369bba898 moviing license file to markdown file type
c423e392dcf2193f9a2b6ced1af51e6275c52cca removing dummpy file
acf06e8d0b8ba90c0d61a4f76942e6928f3334e9 dummy file to be removed
fa38ac8a8f6631d65c6455ea82accff617292cd0 updated contributing file
f0a11ab36efaf93a0d45ec78fca7032fc0bda585 update .gitignore file to ignore log and bin files
2f9fb33c4123b0a8a3e7ffc1a209293f4dd39f79 adding contributing and updating readme
5d430bbfa870532547da51fdf8e3cf6370a05067 update files commit 2
e2d92139ab6eb967ef9c35c9d83385ffa2dfc43a Initial Commit
Filtering Commits or Revision Selection
Short SHA1
A commit is nothing but a snapshot of the repository at that particular point in time. There might be situation wherein we want to know what that commit is all about or range of commits are all about. Let us try to see how we can filter on the required commits using some of the git tools.
A commit id is nothing but a 40 character’s SHA1 Hash. Also a commit can be referred using the only a few characters of the SHA1 hash which are unique within your project. For example we can refer to a commit using the first 7 characters and show w its details as shown below.
[admin@fedser learngit]$ git show f9c7919
commit f9c7919761b8a7be7e4a634d127b1b9369bba898
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Tue May 2 14:59:03 2023 +0530
moviing license file to markdown file type
diff --git a/license.txt b/license.md
similarity index 100%
rename from license.txt
rename to license.md
A branch always refers to the last commit that happened on it. So if you look at the branch details it will show the last commit details as shown below.
[admin@fedser learngit]$ git show main
commit 762f6a031fe6c19e3ead49d7693f9951288c4451 (origin/main, main)
Merge: 6a3ea27 1704b5c
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Tue May 2 18:15:11 2023 +0530
This is an update to same file with conflict resolved
diff --cc chapter4/GitInternals.txt
index 90c5137,2490856..48c9258
--- a/chapter4/GitInternals.txt
+++ b/chapter4/GitInternals.txt
@@@ -1,2 -1,2 +1,2 @@@
GitInternals
- This is an update to same file on master branch
-This is an update to same file on edition branch
++This is an update to same file with conflict resolved
Reference Logs
Git keeps a reference of where the HEAD and branch references are pointing to during the whole lifecycle of the project. This will help us identify the details of the references at a particular time as shown below. It basically acts like history log for all the activities that happened locally on that specific machine and are captured.
[admin@fedser learngit]$ git reflog
a4883be (HEAD -> edition1, origin/edition1) HEAD@{0}: commit: update .gitignore file
762f6a0 (origin/main, main) HEAD@{1}: checkout: moving from main to edition1
762f6a0 (origin/main, main) HEAD@{2}: Branch: renamed refs/heads/master to refs/heads/main
762f6a0 (origin/main, main) HEAD@{4}: commit (merge): This is an update to same file with conflict resolved
6a3ea27 HEAD@{5}: checkout: moving from edition1 to master
1704b5c HEAD@{6}: commit: This is an update to same file on edition branch
caa75f0 HEAD@{7}: checkout: moving from master to edition1
6a3ea27 HEAD@{8}: commit: This is an update to same file on master branch
...
We can look at reference log entry of the HEAD position 10 commits prior as shown below.
[admin@fedser learngit]$ git show HEAD@{10}
commit caa75f0783e83ddb724766d7f22c4c3147061687
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Tue May 2 17:50:28 2023 +0530
created chapter4
diff --git a/GitInternals.txt b/GitInternals.txt
deleted file mode 100644
index aa48b59..0000000
--- a/GitInternals.txt
+++ /dev/null
@@ -1 +0,0 @@
-GitInternals
Parent Commit Reference
Let say we want to get the parent commit details for a particular commit id we can do the following. A commit id followed by a ^ will show its parent.
[admin@fedser learngit]$ git show f9c7919^
commit c423e392dcf2193f9a2b6ced1af51e6275c52cca
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Tue May 2 14:48:30 2023 +0530
removing dummpy file
diff --git a/remove.txt b/remove.txt
deleted file mode 100644
index 0281bee..0000000
--- a/remove.txt
+++ /dev/null
@@ -1 +0,0 @@
-dummy file to be removed
We can also use ~ which basically means the first parent of first parent of first parent in the below example.
[admin@fedser learngit]$ git show caa75f0~~~
commit f9c7919761b8a7be7e4a634d127b1b9369bba898
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Tue May 2 14:59:03 2023 +0530
moviing license file to markdown file type
diff --git a/license.txt b/license.md
similarity index 100%
rename from license.txt
rename to license.md
Commit Ranges
These play a very important role in identifying the range of commit id that are present on particular branch but not present on the other branch. So here in the below example i have a commit in edition1 branch which is not yet merged with the main branch as you can see from the below output.
[admin@fedser learngit]$ git log main..edition1 # outputs the list of commits present on edition1 branch but not merged into main branch yet
commit a4883be13a249e679b385b441e67ad3f33cc961f (HEAD -> edition1, origin/edition1)
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Wed May 3 13:55:58 2023 +0530
update .gitignore file
Let us switch to main branch and merge the edition1 commit changes as shown below.
[admin@fedser learngit]$ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
[admin@fedser learngit]$ git merge edition1
Updating 762f6a0..a4883be
Fast-forward
.gitignore | 1 +
1 file changed, 1 insertion(+)
Now if you look at the commit range again from the edition1 branch there should not be any commit id that is still not merged with the main branch.
[admin@fedser learngit]$ git switch edition1
Switched to branch 'edition1'
Your branch is up to date with 'origin/edition1'.
[admin@fedser learngit]$ git log main..edition1
[admin@fedser learngit]$
The branch can be either the remote branch or local branch. Let us see this with an example where in right now we just merged the local main branch with a edition1 branch. But now our local main branch has a change which is not yet present on the remote main branch.
[admin@fedser learngit]$ git log origin/main..HEAD # show the current branch changes which are not yet pushed to remote main branch
commit a4883be13a249e679b385b441e67ad3f33cc961f (HEAD -> edition1, origin/edition1, main)
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Wed May 3 13:55:58 2023 +0530
update .gitignore file
The same two dot command can be written in the following format.
[admin@fedser learngit]$ git log HEAD ^origin/main # present in HEAD but not in origin/main
commit a4883be13a249e679b385b441e67ad3f33cc961f (HEAD -> main, origin/edition1, edition1)
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Wed May 3 13:55:58 2023 +0530
update .gitignore file
[admin@fedser learngit]$ git log HEAD --not origin/main # present in HEAD but not in origin/main
commit a4883be13a249e679b385b441e67ad3f33cc961f (HEAD -> main, origin/edition1, edition1)
Author: novicejava1 <sudhirbhoga@gmail.com>
Date: Wed May 3 13:55:58 2023 +0530
update .gitignore file
Hope you enjoyed reading this article. Thank you..
Leave a Reply
You must be logged in to post a comment.