Understanding Chain of Commits | Github

It will carry discussion on general topics. It may vary from ece, cse, coding and anything which helps in both growth and development
Post Reply
User avatar
Aisangam
Site Admin
Posts: 27
Joined: 24 Mar 2019
Location: Delhi, India
Contact:

Understanding Chain of Commits | Github

Post by Aisangam » 27 Sep 2019

Hello Guys, This post is very important for those who wishes to learn about version control system (VCS). I am making you understand chain of commits in github. You commits a lot of code everyday and if you wish to reverse a commit you may easily do it using git reset HEAD and git checkout command. Have you ever wonder how git maintains the reference to all the commits you may made.

This is the reason I am writing about this so that deep understanding of git commands could be developed. Git stores every commit in form of dictionary where commit id is the key and commit is the value. Whenever one make new commit, this new commit is referenced to the previous commit and in this way git maintains the integrity of the commits. Let us understand this with some example code and screenshot.

Suppose you are on the local system and you created a folder and change directory to the newly created folder. You can make it work like remote repository by issuing the below command.

Code: Select all

git init
This command will create a hidden folder called as .git which has the below files in it

Code: Select all

branches  COMMIT_EDITMSG  config  description  HEAD  hooks  index  info  logs  objects  refs


Now create a new file inside the newly created folder called as file_chain_demo.txt. This is demo file and you can name any file you wish to name it. Now it is the time to stage this file from the unstaging area by hitting the below command

Code: Select all

git add .
In this way, file is added to the stagging area. Now it is the time to commit the file by issuing the below command

Code: Select all

git commit -m "first_commit"
This is the first commit which I have made, You can also check the commit object as well as commit id using the below command

Code: Select all

git log --oneline
Suppose, I got the below result after this command

Code: Select all

ac325ac first_commit 
It is the time to create a new commit. Please open the file file_chain_demo.txt and add some text on it and commit it again using the above process i.e stagging it and then commiting it. I leave these operation to be done by the readers so that they may understand the things better as well as do some practice.

Now when you run the command git log --oneline again, you will get the below result

Code: Select all

6c47033 second_commit
ac325ac first_commit
As I mentioned in the starting, the new commit has the parent as the previous commit. Now let us see whether this is exactly the same as I said. Please run the below command to see the parent of second_commit

Code: Select all

git cat-file -p 6c47033
Output of this command would be as below. Output will vary accordingly as you will be doing the same on your pc but reference would be to the previous commit.

Code: Select all

tree b3aa04e95aa7a7b9c65837adddfe7d34905451b5
parent ac325acdeb0440253df820e0b84a52dc753b83f9
author AISangam <aisangamofficial@gmail.com> 1569588174 +0530
committer AISangam <aisangamofficial@gmail.com> 1569588174 +0530

second_commit
Please see the output from the screenshot https://bit.ly/2nhfrch also.

What one can see from the above output

From the above output, we can see that commit id 6c47033 which is the second commit I have made has parent ac325ac which is the first commit. In this way integration is made and commits are referred.

I hope users/readers/developers/visitors has learnt some good insights from this answer. If you want to learn more about version control system , you may also visit the below links which are the posts written by AI Sangam (Data Science company).

https://www.aisangam.com/blog/getting-s ... i-sangam/
https://www.aisangam.com/blog/gitignore ... ai-sangam/

Post Reply