Git Log
The git log command shows the history of commits, from the newest to the oldest. It is the way to answer “who changed this, when and why?”.
Syntax
git log
git log --oneline
git log -n 3
git log --stat
git log -p
git log --author="name"
git log --grep="word"
Make a small history to practice
Basic log
$ git log
commit 2f62a896574813946380bdd086cfe3d467cedc5e (HEAD -> main)
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 11:31:00 2026 +0000
Fix typo in books list
commit 617155c93e8b42f6ad30b7cc3bae7ee734c67c0e
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 11:17:00 2026 +0000
Add magazines list
commit 483372d698640172f53444c5b102e0b672f330e8
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 10:56:00 2026 +0000
Add second book
commit 08e1c3a4ffe119cf3ff2b914640a10e2c4023d25
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 10:42:00 2026 +0000
Add first book
Each commit shows the full hash, the author, the date and the message. The commit on the top is the latest one. HEAD -> main tells that you are on the main branch at this commit.
One line per commit
$ git log –oneline
2f62a89 (HEAD -> main) Fix typo in books list
617155c Add magazines list
483372d Add second book
08e1c3a Add first book
Limit the number of commits
$ git log -2 –oneline
2f62a89 (HEAD -> main) Fix typo in books list
617155c Add magazines list
See which files changed in each commit
$ git log –stat –oneline
2f62a89 (HEAD -> main) Fix typo in books list
books.txt | 1 +
1 file changed, 1 insertion(+)
617155c Add magazines list
magazines.txt | 1 +
1 file changed, 1 insertion(+)
483372d Add second book
books.txt | 1 +
1 file changed, 1 insertion(+)
08e1c3a Add first book
books.txt | 1 +
1 file changed, 1 insertion(+)
See the exact changes (patch)
$ git log -p -1
commit 2f62a896574813946380bdd086cfe3d467cedc5e (HEAD -> main)
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 11:31:00 2026 +0000
Fix typo in books list
diff –git a/books.txt b/books.txt
index 6d86d80..a564d9a 100644
— a/books.txt
+++ b/books.txt
@@ -1,2 +1,3 @@
Book 1
Book 2
+Book 3
Filter the log
$ git log –oneline –grep="book"
2f62a89 (HEAD -> main) Fix typo in books list
483372d Add second book
08e1c3a Add first book
$ git log –oneline –author="Robin"
2f62a89 (HEAD -> main) Fix typo in books list
617155c Add magazines list
483372d Add second book
08e1c3a Add first book
$ git log –oneline — magazines.txt
617155c Add magazines list
--grepsearches in the commit messages.--authorshows commits of a person.- A path after two dashes shows only the commits which changed that file.
--since="2 weeks ago"and--untilfilter by date.
Custom format
$ git log –pretty=format:"%h | %an | %s"
2f62a89 | Robin Sharma | Fix typo in books list
617155c | Robin Sharma | Add magazines list
483372d | Robin Sharma | Add second book
08e1c3a | Robin Sharma | Add first book
| Placeholder | Meaning |
|---|---|
%H / %h | Full hash / short hash |
%an | Author name |
%ae | Author email |
%ad | Author date |
%s | Commit message title |
Show one commit
$ git show –stat –oneline HEAD
2f62a89 (HEAD -> main) Fix typo in books list
books.txt | 1 +
1 file changed, 1 insertion(+)
Graph of branches
When you have branches, --graph draws the lines for them. We will see it with a real example in the merge chapter.
git log --oneline --graph --decorate --all
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |