Git

What is Git Commit Hash and How to Get It?

Git is a decentralized tracking tool that enables users to make changes in their development project source code files. After that, these changes are added to the local repository. Then, they can make them publicly available to project members. For saving these changes, the “git commit” command can be used that represents the modifications. When these changes are added through this command, a unique id is assigned which is known as a commit hash.

The outcomes from this guide are:

What is Commit Hash in Git?

In Git, the commit hash is the unique commit id or value that is automatically generated and assigned to commits whenever a new commit is made in the Git repository. A hash is used while checking out commits, merging commits, creating branches, restoring commits, and many more.

How to Retrieve Commit Hash in Git?

Follow the provided steps to retrieve the commit hash in Git:

  • Redirect to the Git local repository.
  • Create and edit text files simultaneously.
  • Push the file from the Git working area to the tracking index.
  • Commit changes for saving into the Git repository.
  • Check the Git log history by running the “git log –oneline” command.

Step 1: Move to the Local Repository

Initially, use the “cd” command along with the local repository path:

cd "C:\Users\nazma\Git\demo4"

 

Step 2: Create and Edit File

Next, execute the “echo” command along with the file name and content:

echo "its my second text file">> file2.txt

 

Step 3: Track File

Now, add the newly created file to the staging index by running the provided command:

git add file2.txt

 

Step 4: Save Changes

Then, save changes into the repository by using the provided command along with the commit message:

git commit -m "new file added"

 

Here, the “-m” option indicates the commit message. According to the below-given output, we have successfully added a file to the repository:

Step 5: Get Commit Hash

Finally, run the “git log –oneline” command to retrieve the commit hash:

git log --oneline

 

As you can see, all the commit hash has been displayed successfully and currently HEAD pointing to the below-highlighted SHA-hash:

How to Use Commit Hash in Git?

The commit hash can be used for several purposes, such as:

  • For reverting changes.
  • For checking out the particular commit.
  • For creating branches.

Revert Changes using Git Commit Hash

To revert changes, first, gets the particular commit hash by running the provided command:

git log --oneline

 

From the below-given output, we have selected the “142b7ef” commit hash for reverting the most committed changes:

Then, execute the “git reset” command along with the copied commit hash:

git reset 142b7ef

 

Now, display the Git log history by using the following command:

git log --oneline

 

According to the below-given output, the most recent changes have been reverted and HEAD pointing to the specified commit hash:

Checkout to the Particular Commit

Sometimes users want to know detailed information about a particular commit, such as which file or changes have been added through this commit and many more. For that purpose, first, viewed the Git log history by running the given command:

git log --oneline

 

Here, we have selected the “3dc6da5” commit hash for further process:

Now, run the git checkout“ command along with the copied commit SHA-hash:

git checkout 3dc6da5

 

According to the below-given output, currently HEAD pointing to the specified commit hash:

For verification, view the Git log history by running the following command:

git log --oneline

 

Create Branches

The Git commit command can also be used for creating new branches in the Git root directory. For that purpose, first, get the commit hash by running the “git log –oneline” command:

git log --oneline

 

Now, select the desired commit hash from the given output. For instance, we have chosen the below-highlighted commit hash:

After that, execute the “git branch” command with the new branch name and preferred commit hash:

git branch demobranch 9f74102

 

Now, use the provided command to display all local branches:

git branch

 

It can be seen that the “demobranch” has been created successfully using the Git commit hash:

That’s all! We have described the Git commit hash and its usage.

Conclusion

The commit hash is the unique commit id that is automatically generated and assigned to commits whenever a new change has been saved in the Git repository. The commit hash is used for checking out, merging commits, creating branches, and restoring commits. In this guide, we have demonstrated the Git hash.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.