How to get the hash of last commit in Git?

In this article you will see that how you can get the hash of current commit and you will also see what are git logs how you get any commit deatils using git log

 

How to get hash of last commit?

You can use the following command to get the hash for the current commit in Git:

git rev-parse HEAD

The output will be the hash of the current commit, which is the latest commit on the current branch. The HEAD argument specifies that you want the hash for the current branch head.


How to get details of last commit?

You can use the following command to get the details of the last commit in Git:

 

git log -1

The -1 argument limits the number of commits to show to 1, so only the latest commit will be displayed. The output will include the commit hash, author, date, and commit message.

If you want to display specific information about the last commit, you can use options such as:

 

git log -1 --pretty=format:"%h %an %ad %s"

The  --pretty=format:<string> option specifies the format of the output. The %h format specifier is the abbreviated hash, %an is the author name, %ad is the author date, and %s is the subject (commit message).
 


what is git log?
 

The git log command is used to display the commit history for a Git repository.

By default, git log will display a list of all commits, showing the commit hash, author, date, and subject (commit message) for each commit.

 

For example:

git log

There are many options available to customize the output of git log, such as:

 

  • --pretty: Specify the format of the commit history output. For example, --pretty=oneline will display each commit on a single line.
  • --reverse: Display the commit history in reverse order (from newest to oldest).
  • --since: Show only commits that were made after a specified date.
  • --until: Show only commits that were made before a specified date.
  • --author: Show only commits made by a specified author.
  • --grep: Show only commits with a specified string in the commit message.

 

For more information and options, you can refer to the Git documentation: https://git-scm.com/docs/git-log

 

 


Tags:

git

Share:

Related posts