A git repository contains many commits depends on the project task. Sometimes the git user needs to clone a repository with the existing commits and may require to know the previous git commit history. `git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first. The different ways to view the commit history of the git repository by using the `git log` command have been explained in this tutorial.
Install GitHub Desktop.
GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.
Create a GitHub account.
You will require to create a GitHub account to check the output of the commands in the remote server.
Create a local and remote repository.
You have to create a local repository and publish the repository in the remote server to test the commands used in this tutorial. Go to the local repository folder to check the commands used in this tutorial.
View commit history:
The commit history can be viewed in different ways by using the `git log` command. A local repository named bash has been used in this tutorial to test the commands used in this tutorial. Run the following command to view the commit history of the repository. The output will contain the commit SHA value, username and email of the author, commit date and time, and the commit message.
The following output shows that two commits have been done in the current repository by the same author on the same date but at different times. Two different commit SHA values have been created for two different commits.
The commit history can be viewed based on the commit SHA value using the `git show` command. Run the following command to view the commit history of the given commit SHA.
The output shows the details of the commit history based on the particular commit SHA value. The other information such as the filename, file mode, index value, and the content of the committed file have displayed here. It helps the git user to identify the necessary commit that the user wants to modify or correct.
The -p or –patch option is used with the `git log` command to display the difference in each commit. The view of the log entries can be limited by defining a specific number with this option. Run the following command to display the latest commit history.
The following output will appear after executing the above command. It looks like the previous output because only one commit history has been retrieved here. When the multiple commit history is displayed by increasing the limit value, the difference between the commit history will be cleared.
The –stat option is used with the `git log` command to display some state information with the commit history.
The following output shows that the .gitattributes file has been inserted in the initial commit. The bash-basic-tutorial.sh file has been inserted in the second commit of the repository with the other commit information.
The –online option of the `git log ` command displays the commit history in a single line with the short commit SHA value. When the repository contains many commits, the `git log` command will long. The output of all commit history can be displayed in the short form by using the –online option that helps to view a large number of commit history properly.
The following output will appear after executing the above command. Here, the first 7 characters of the commit SHA value have displayed for each commit.
The –pretty is another option of `git log` to display the log output in various formats. This option can be used by assigning different types of values, and the output of the command depends on the assigned value. Run the following command to view the commit history in the short form by assigning online for the –pretty option.
The output of the above command is similar to the previous command except for the commit SHA value. Here, the full commit SHA value has displayed.
Many placeholders exist, which are defined by the character literals to format the log output. The log output can be formatted according to the user’s requirement by defining the format with the proper string value in the –pretty option. Run the following command to print the commit SHA value in the short form, the git username, and the commit message in the way that has been defined in the string. Here, %h is used to display the commit SHA value in the short form, %an is used to display the author’s username, and %s is used to display the commit message.
The following output will appear after executing the above command.
The commit history is an essential part of any git repository. The git user can get an idea about each commit of the repository by viewing the commit history and making the necessary modification. The different ways to view the commit history have been shown in this tutorial using a demo repository.