Get job ready skills with Codenga     |       Career Paths up to 50% OFF     |        Limited time only

2d 09h
close
Cart icon
User menu icon
User icon
Lightbulb icon
How it works?
FAQ icon
FAQ
Contact icon
Contact
Terms of service icon
Terms of service
Privacy policy icon
Privacy Policy
Zdjęcie główne artykułu.

Git - viewing history

While working with a repository we sometimes need to check the history of commits. Git offers several different ways of browsing commit history. In this short article we will take a look at some of the most useful history-related commands. Let’s start!

git log command

The easiest way to display commit history is to use a git log command. It will display a list of commits sorted from top to bottom. The newest commit will be displayed at the top.

By default, the git log command will display all commits. If you want to limit the number of results, do it this way:

git log -2

Just use a suitable flag (eg., -2) . In the above example we want to display two latest commits. And here is the result:

commit 009e4904bfb5bb3965a3274637270dcffe68eb8e (HEAD -> feature, master)
Author: Admin Admin
Date: Mon Aug 22 10:54:12 2022 +0200

add dots

commit 87be722d5f42faa6f30f92a0bc18334ba13bc192 (tag: v5.4, tag: v3.0, tag: v2.0)
Author: Admin Admin
Date: Thu Aug 18 14:47:44 2022 +0200

Add third line

As you can see, it works as intended. We got a list of the latest commits.

Useful params

Sometimes we want to have a quick glance at the history of commits. You can add a simple param to the git log command:

git log --oneline

And here is the result:

009e490 (HEAD -> feature, master) add dots
87be722 (tag: v5.4, tag: v3.0, tag: v2.0) Add third line
02273b8 Revert "Add third line"
c2af0e5 Add third line
eb19f98 Add second line
413db8e Add first line
f22e23b init

We got a simple list of commits. Sometimes that’s all we need.

On the other hand, sometimes we need more detailed info. You can use this combo of params:

git log -2 -p

Note the -p param. And here is the result:

commit 009e4904bfb5bb3965a3274637270dcffe68eb8e (HEAD -> feature, master)
Author: Admin Admin
Date: Mon Aug 22 10:54:12 2022 +0200

add dots

diff --git a/codenga.txt b/codenga.txt
index ab7c514..286b732 100644
--- a/codenga.txt
+++ b/codenga.txt
@@ -1,3 +1,4 @@
First
Second
Third
+...

There is only a single commit shown. We did it on a purpose - just to make this result more readable. You should get the idea anyway.

We got a detailed list of commits. There are pieces of info like the name of a file modified, what was changed in each file etc.

We just skimmed a surface as long as the git log command is concerned. That command comes with an extensive list of useful params. You can find all of them in the official Git documentation. In this short article we simply focused on the most common uses of this very handy command.