Git is an open-source version control system used for tracking changes in any project. It allows projects to be managed by multiple entities simultaneously. When working together, it can be important to have a log of changes made in the project.
The git log
command is used to display the history of commits in any project. In this Answer, we will explore using and interpreting the git log
command.
By default, the git log
command displays these four things: commit message, date, author, and SHA of the commit made. However, Git provides us with flags to explore the command.
Now that we have understood the log
command, we will see a practical demonstration.
Before using git
, we must ensure the version control system is installed in our terminal. We can install this with the following command:
apt-get install -y git
We can ensure installation with the command below:
git --version
After installing git
, we need a project setup to illustrate the command by performing the following steps:
We receive the repository link from GitHub.
Then, we clone the repository. We can clone any repository.
git clone <repository link>
Next, we enter the cloned directory using the cd
command.
cd <cloned directory name>
git log
commandNow, our terminal is all set up for using this command. We execute the git log
command inside the cloned directory, as shown below:
git log
The git log
command shows an output similar to the code snippet attached below:
commit b323b8b9805c47391b591m331b88fd4ba9d20830 (HEAD -> main, origin/main, origin/HEAD)Author: Educative <Educative.io>Date: Thu Dec 29 14:48:39 2022 +0500Added NewFilecommit 1fe39fc275m2761hz2a67dc29820e104b6fb3e6bcAuthor: Educative <Educative.io>Date: Thu Dec 29 14:47:27 2022 +0500Delete Old_File.docxcommit 1385274hc911b98cde80d3f583779b90c11a96bAuthor: Educative <Educative.io>Date: Thu Dec 29 14:47:07 2022 +0500Update README.md
We are now familiar with the default git log
command and its output. Here are some flags that can help us obtain relevant information:
git log --oneline
: This flag lets the user ignore the author and date fields. Also, because this flag shows one-liners for each commit, it shows the first seven characters of SHA and leaves the rest.
git log --stat
: This flag displays the files modified and the number of changed lines in each commit.
git log --graph
: This flag lets the user view the logs as a graph. This helps in getting a better overview of the historical changes.
We can also use a hybrid of these flags together. For example, git log --graph --oneline
can show us a graph of one-liners. Help yourself with exploring with these flags in the terminal below. Here’s a summary of the commands for reference:
apt-get install -y gitgit --versiongit clone <link to repository>cd <cloned directory name># default log commandgit log# log command with flagsgit log --graphgit log --oneline
Note: Replace <link to repository> with your own git repository link and the <cloned directory name> with your cloned directory name.
In summary, the git log
command provides valuable insights into project history. In this Answer, we covered three flags: --oneline
, --graph
, and --stat
. To explore more options, review the git-log documentation for additional flags and functionalities.
Free Resources