Git

What is a Git Commit ID?

Git is a decentralized versioning control system that allows developers to make changes in their project source code files. Then, add them to the local repository. After that, developers make them publicly available to everyone. These changes can be added by the “git commit” command that represents the modifications.

This article will explain:

What is Git Commit?

Commits are snapshots of project code at a specific point in the past. Developers can view the old changes in project code that were replaced with new changes using commits. Additionally, it is useful for collaboration with other project members because it contains changes made by them.

What is Git Commit ID?

Git Commit ID is the unique SHA-hash value generated automatically and assigned to commits whenever a new commit is made to the repository. Commit id is used while merging commits or checking out files from different commits.

How to Commit in Git?

To commit added changes from the staging area to the local repository, the “git commit -m “<commit-message>” ” command can be used. After running this command, new files or changes get added to the local repository.

Check out the following steps to explore the process of committing in Git!

Step 1: Switch to Local Git Repository
First, use the below-provided command and redirect to the local directory:

$ cd "C:\Git\demo_Repo"

Step 2: View Repository Content
Then, view the list of existing content of the repository with the help of the “ls” command:

$ ls

The below screenshot indicates that the working repository contains only “abc.txt” file:

Step 3: Make Changes in File
Now, utilize the “echo” command, and add some content to the existing file:

$ echo "Hi! This is ABC file" >> abc.txt

Step 4: Push Changes
Next, track the added changes to the Git staging area by running the below-stated command:

$ git add abc.txt

Step 5: Commit New Changes
Execute the provided command to commit new staged changes:

$ git commit -m "ABC file updated"

According to the below-provided output, it can be observed that the changes have been committed successfully:

Let’s move ahead and see how to get the commit id in Git.

How to Find Commit ID Using Git Commands?

Multiple Git commands are available to get the commit id, such as:

Method 1: How to Find Commit ID in Git Using the “git rev-parse” Command?

To get the most recent commit id, execute the following command:

$ git rev-parse HEAD

Here, the “b7343e5e…..” is the latest commit id:

Method 2: How to Check Commit ID Using the “git show” Command?

In order to find the detailed information of the latest commit, including the commit id and commit message, type out the following command:

$ git show -s

Here, the “-s” option is used to retrieve the detailed information of the latest commit. In the below screenshot, the highlighted information is the commit id:

Method 3: How to Check Commit ID Using “git log –oneline” Command?

Use the “git log –oneline” command to view the list of all commits, including commit id and messages:

$ git log --oneline

The below output shows the commit SHA-hashes of all existing repositories commits:

We have briefly explained the Git commit id and the different ways of finding it.

Conclusion

Git commit id is a unique SHA-hash value that is generated automatically whenever new changes are pushed to the local repository through committing. Multiple Git commands are used to get the commit id of the added commits, such as the “git rev-parse HEAD” command, “git show -s” command, and “git log –oneline” command. This article illustrated the Git commit, commit id and the method of getting Git SHA-hashes in Git.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.