fbpx

Explore ‘git log’ feature and power

Git is a powerful tool. It provides many features to manage the repository in a better way. There are many git commands which can be used to manage the repo, one of them is ‘git-log’ to show commit logs.

In general ‘git log ‘ is use to show commit logs, But it can be used it more than this. Let explore it.

Show commit logs:

git log
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

commit a9dfb9cb9dd0c5989cc1f293ec8eac36a9d65adf
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:35:18 2018 +0530

    add local file(.txt, mp3 etc) search feature

commit 4bc8c327ea140bdd661f7ee047792c9b656b1efc
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:31:15 2018 +0530

    add movie details search feature

commit 8f78558af99a413232ffac37ab99f41887f5ad85 (tag: v1.0)
Author: Singhak <singhak.in@gmail.com>
Date:   Sun Mar 4 15:15:08 2018 +0530

    add search feature

commit dc0e6c318886bc3bbb74eabeeeb9b3c953a813d7
Author: Singhak <singhak.in@gmail.com>
Date:   Sun Mar 4 10:55:33 2018 +0530

    update read.me

Limit the number of commit:

Limit the number of commits to output.

git log -n // n is number

Show last two commit:

hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log -2
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features
git log --max-count=<number>
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --max-count=3
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

commit a9dfb9cb9dd0c5989cc1f293ec8eac36a9d65adf
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:35:18 2018 +0530

    add local file(.txt, mp3 etc) search feature

To show commit for specific file(s)

git log <file_name> <file_name>
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log -2
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log alienUI.py
commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

commit 8f78558af99a413232ffac37ab99f41887f5ad85 (tag: v1.0)
Author: Singhak <singhak.in@gmail.com>
Date:   Sun Mar 4 15:15:08 2018 +0530

    add search feature

To show log commit with file name:

git log --name-only
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --name-only
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

README.md

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

.gitignore
README.md
alienUI.py
requirement.txt

commit a9dfb9cb9dd0c5989cc1f293ec8eac36a9d65adf
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:35:18 2018 +0530

    add local file(.txt, mp3 etc) search feature

localfind.py

To show log commit with file name and status:

git log --name-status
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --name-status
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

M       README.md

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

A       .gitignore
M       README.md
M       alienUI.py
M       requirement.txt

commit a9dfb9cb9dd0c5989cc1f293ec8eac36a9d65adf
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:35:18 2018 +0530

    add local file(.txt, mp3 etc) search feature

A       localfind.py

To show log of the specific author:

Limit the commits output to ones with author/committer header lines that match the specified pattern (regular expression). With more than one --author=<pattern>, commits whose author matches any of the given patterns are chosen

git log --author=<pattern> // author_name is case sensitive
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --author='Anil Singh'
commit b8fac442f0f156e77648f3645dd397bc6ad1517c
Author: Anil Singh <singhak.in@gmail.com>
Date:   Fri Mar 2 19:11:11 2018 +0530

    Create productSearch.py

    search product on Amazon/Flipkart

commit 829feb01c43bc96279bd07f00d57f09870f652c2
Author: Anil Singh <singhak.in@gmail.com>
Date:   Fri Mar 2 19:10:12 2018 +0530

    Create newfeedparse.py

    parse news rss feed

commit b663fffbd70d4f9b694519ee0ee25abea00abf60
Author: Anil Singh <singhak.in@gmail.com>
Date:   Fri Mar 2 19:08:38 2018 +0530

    Create news.py

    get new items

Show only commit id and commit message:

git log --pretty=oneline
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --pretty=oneline
b8fac442f0f156e77648f3645dd397bc6ad1517c Create productSearch.py
829feb01c43bc96279bd07f00d57f09870f652c2 Create newfeedparse.py
b663fffbd70d4f9b694519ee0ee25abea00abf60 Create news.py
6aff6de584aa5a77565822d30fc436031d60cdbd Create findJoke.py
3b07063c96476dee5b41a7eee6dcb1b30f81003c Create alienUI.py
852d06f5cb277deebadffa4da6c7e9766fe5a0a5 Create README.md

You can try other than oneline to get the different outputs of the git log. like short, medium, full, fuller, reference, email, raw

Show only full commit Id:

git log --pretty=format:%H
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log -1 --pretty=format:%H
1fcd0d283a2944368a79b52c0a91387b7e32ad4d

Show short commit ID:

git log --pretty=format:%h
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log -1 --pretty=format:%h
1fcd0d2

Get author name of Specific commit id:

git log --pretty=format:%an <commit_id>
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --pretty=format:%an  b8fac44
Anil Singh

Get author name and commit id:

git log --pretty='format:%an %h'
git log --pretty='format:%an %H'
git log --pretty='format:%h %an'
git log --pretty='format:%H %an'
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log -1 --pretty='format:%an %h'
Anil Singh b8fac44
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log -1 --pretty='format:(%an) %h'
(Anil Singh) b8fac44

// Try Yourself
git log –name-status –pretty=medium

Filter the log on the basis of time:

// Show commits older than a specific date.
git log --since=<date>
git log --after=<date>
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --since="2018-03-05"
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

commit a9dfb9cb9dd0c5989cc1f293ec8eac36a9d65adf
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:35:18 2018 +0530
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --after="2018-03-05"
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

commit a9dfb9cb9dd0c5989cc1f293ec8eac36a9d65adf
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:35:18 2018 +0530

    add local file(.txt, mp3 etc) search feature

// Try yourself
git log –since=”2 weeks ago”
git log –after=”yesterday”

git log –name-only –after=”yesterday”
git log –name-status –after=”yesterday”

git log --until=<date>
git log --before=<date>
// Show commits older than a specific date.
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --until="2018-03-05"
commit 8f78558af99a413232ffac37ab99f41887f5ad85 (tag: v1.0)
Author: Singhak <singhak.in@gmail.com>
Date:   Sun Mar 4 15:15:08 2018 +0530

    add search feature

commit dc0e6c318886bc3bbb74eabeeeb9b3c953a813d7
Author: Singhak <singhak.in@gmail.com>
Date:   Sun Mar 4 10:55:33 2018 +0530

    update read.me

//Try yourself
git log –after=”2018-12-1″ –before=”2019-6-18″

hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --before="2018-03-03"
commit b8fac442f0f156e77648f3645dd397bc6ad1517c
Author: Anil Singh <singhak.in@gmail.com>
Date:   Fri Mar 2 19:11:11 2018 +0530

    Create productSearch.py

    search product on amazon/flipkart

commit 829feb01c43bc96279bd07f00d57f09870f652c2
Author: Anil Singh <singhak.in@gmail.com>
Date:   Fri Mar 2 19:10:12 2018 +0530

    Create newfeedparse.py

To get commit details with file modification details

git log --stat
hp@Singhak MINGW64 /e/Projects/02-Python/Desktop-Assistant (master)
$ git log --stat
commit 1fcd0d283a2944368a79b52c0a91387b7e32ad4d (HEAD -> master, tag: v2.0, origin/master, origin/HEAD)
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 22:03:28 2018 +0530

    update readme

 README.md | 7 +++++++
 1 file changed, 7 insertions(+)

commit aea3857637b3344b608c360110acaa5607646385
Author: Singhak <singhak.in@gmail.com>
Date:   Mon Mar 5 21:45:57 2018 +0530

    update readme and add new features

 .gitignore      |  2 ++
 README.md       | 14 +++++++++-----
 alienUI.py      | 19 ++++++++++++++++---
 requirement.txt |  5 ++---
 4 files changed, 29 insertions(+), 11 deletions(-)

Reference: