Git Examples: Searching the Git History

Git Examples: Searching the Git History

Last updated:
Git Examples: Searching the Git History
Source
Table of Contents

Show files names in git log

Use git log --name-status:

Example output:

$ git log --name-status 
commit febc6920f2f043eb9291d9a2de9edcfa08bcafb0 (HEAD -> master, origin/master, origin/HEAD)
Author: Felipe <felipe@localhost>
Date:   Sun Oct 31 23:59:18 2021 -0300

    Update multiple files

M       _posts/2014-06-19-python-environment-examples-and-gotchas-pip-virtualenvs-etc.markdown
M       _posts/2016-03-25-python-environment-overview.markdown
M       _posts/2017-11-15-package-a-python-project-and-make-it-available-via-pip-install.markdown
M       _posts/2020-03-24-numpy-display-options-examples-and-reference.markdown
M       sitemap-latest.xml

commit 427385702e7d8d7d8bb971b9d9c3af563cf26b98
Author: Felipe <felipe@localhost>
Date:   Sun Oct 31 19:16:17 2021 -0300

    Update multiple files

M       _posts/2021-04-25-bash-examples-redirecting-output-error-input-etc.markdown
D       _posts/2021-06-25-bash-examples-set-builtin.markdown
M       _posts/2021-10-31-bash-options-with-set-examples-and-reference.markdown
M       sitemap-latest.xml

Show file contents in git log

Use git log --patch to include full diff information for all modified files in the output of git log:

Example output:

$ git log --patch
commit febc6920f2f043eb9291d9a2de9edcfa08bcafb0 (HEAD -> master, origin/master, origin/HEAD)
Author: Felipe <felipe@localhost>
Date:   Sun Oct 31 23:59:18 2021 -0300

    Update multiple files

diff --git a/_posts/2014-06-19-python-environment-examples-pip-virtualenvs-etc.markdown b/_posts/2014-06-19-python-environment-examples-pip-virtualenvs-etc.markdown
index 1b9ec87..f637beb 100644
--- a/_posts/2014-06-19-python-environment-examples-pip-virtualenvs-etc.markdown
+++ b/_posts/2014-06-19-python-environment-examples-pip-virtualenvs-etc.markdown
@@ -44,8 +44,10 @@ $ virtualenv <your-env-name> --system-site-packages
 $ virtualenv -p python3 <your-env-name>

-## Install package for the currently activated `virtualenv`
+## Install package in current `virtualenv`

+In other wrods, in the **currently activated** virtualenv
+

List commits that changed file, by path

If you know the full path to the file you want to check, just add -- path/to/my/file at the end of the command:

Example:

$ git log --name-status -- _posts/2017-08-06-hierarchies-and-partial-orders.markdown
commit 9f034d7dfacbb9ee10d30f817c7988e0ebb3d7a3
Author: Felipe <felipe@localhost>
Date:   Sun Oct 31 19:05:25 2021 -0300

    Remove file

D       _posts/2017-08-06-hierarchies-and-partial-orders.markdown

commit e2c0c27fedaafc63773a4128bca03c6fe8d5885a
Author: Felipe <felipe@localhost>
Date:   Sun Aug 6 16:29:29 2017 -0300

    Fix typo

M       _posts/2017-08-06-hierarchies-and-partial-orders.markdown

commit 2d3ebf6a448fdd2fd16a31cc717d8e21756f8a78
Author: Felipe <felipe@localhost>
Date:   Sun Aug 6 15:49:34 2017 -0300

    Add new file

A       _posts/2017-08-06-hierarchies-and-partial-orders.markdown

List commits that changed file, by file name or pattern

If you don't know the full path to a file, you can just use a path wildcard:

Example:: get commits that touched any file that ends in orders.markdown:

$ git log --name-status -- *orders.markdown
commit 9f034d7dfacbb9ee10d30f817c7988e0ebb3d7a3
Author: Felipe <felipe@localhost>
Date:   Sun Oct 31 19:05:25 2021 -0300

    Remove file

D       _posts/2017-08-06-hierarchies-and-partial-orders.markdown

commit e2c0c27fedaafc63773a4128bca03c6fe8d5885a
Author: Felipe <felipe@localhost>
Date:   Sun Aug 6 16:29:29 2017 -0300

    Fix typo

M       _posts/2017-08-06-hierarchies-and-partial-orders.markdown

commit 2d3ebf6a448fdd2fd16a31cc717d8e21756f8a78
Author: Felipe <felipe@localhost>
Date:   Sun Aug 6 15:49:34 2017 -0300

    Add new file

A       _posts/2017-08-06-hierarchies-and-partial-orders.markdown

List commits including string in content

To list all commits that made any change containing the given string.

Use git log --name-status -S"some-string".

Example: Return all commits where the string "pandas" was included anywhere, in any file: $ git log --name-status -S"pandas"

$ git log --name-status -S"pandas"
commit ef9f570c326be8568de012c463f195e0214ba030
Author: Felipe <felipe@localhost>
Date:   Sun Aug 29 00:38:05 2021 -0300

    Update sitemap

A       sitemap-latest.xml

commit 6fa22fbcd483593e6b6ec95a75fc20088d266262
Author: queirozfcom <felipe@localhost>
Date:   Tue Jun 29 22:41:33 2021 -0300

    Update pandas display post

M       _posts/2020-03-24-pandas-display-options-examples-and-reference.markdown

commit bae5f2505c391a8b03bc1af86dad75a759a487a5
Author: queirozfcom <felipe@localhost>
Date:   Sat Jun 26 22:45:44 2021 -0300

    Add new post on nltk and scikit-learn

A       _posts/2016-06-06-full-example-on-using-nltk-and-scikit-learn.markdown

List commits that include regex in content

In other words, search commits matching some regex

Use git log --name-status -G"some-regex", using a suitable regex:

Example: Get all commits where the changes match the regex "p(andas|python)": git log --name-status -G"p(andas|ython)".

$ git log --name-status -G"p(andas|ython)"
commit febc6920f2f043eb9291d9a2de9edcfa08bcafb0 (HEAD -> master, origin/master, origin/HEAD)
Author: Felipe <felipe@localhost>
Date:   Sun Oct 31 23:59:18 2021 -0300

    Update multiple posts

M       _posts/2014-06-19-python-environment-examples-pip-virtualenvs-etc.markdown
M       _posts/2016-03-25-python-environment-overview.markdown
M       _posts/2017-11-15-package-a-python-project-and-make-it-available-via-pip-install.markdown
M       _posts/2020-03-24-numpy-display-options-examples-and-reference.markdown
M       sitemap-latest.xml

commit e6b453c3cea9d6f1f54e18f0ea88342ef67a6005
Author: Felipe <felipe@localhost>
Date:   Sun Aug 29 19:06:34 2021 -0300

    Fix typo on post about writing code

M       _posts/2021-08-08-write-code-like-you-will-forget-the-context.markdown

commit ef9f570c326be8568de012c463f195e0214ba030
Author: Felipe <felipe@localhost>
Date:   Sun Aug 29 00:38:05 2021 -0300

    Update sitemap

A       sitemap-latest.xml

Show file contents in commit

Use git show <revision>:path/to/file to view the state of a file right after some commit.

Example: What did foo/bar/baz.txt look like right after commit aa128c8cfbc6f3d3db? Use git show aa128c8cfbc6f3d3db:foo/bar/baz.txt

$ git show aa128c8cfbc6f3d3db:foo/bar/baz.txt
hello, i'm a simple
file with some dummy text

Checkout file from past commit under new name

Say you have identified a version of a single file you want to retrieve from the git history.

To checkout a file from the history under a new name, use git show <revision>:path/to/file > new_file:

Example: retrieve foo/bar/baz.txt as it looked like after commit aa128c8cfbc6f3d3db and save it as new-baz.txt in the current directory:

$ git show aa128c8cfbc6f3d3db:foo/bar/baz.txt > new-baz.txt

Use cat to show file contents:

$ cat new-baz.txt
hello, i'm a simple
file with some dummy text

Compare local file to version in history

To compare the way a file is now to what it was like in the past, just use git diff <revision> path/to-file:

Example: what does foo/bar/baz look like now in comparison to what it was after commit e7e5a8707df7746e? (Answer: the current version of the file has 3 extra lines!)

$ git diff e7e5a8707df7746e foo/bar/baz
diff --git a/foo/bar/baz b/foo/bar/baz
index f89d0f5..15d90d6 100644
index f89d0f5..15d90d6 100644
--- a/foo/bar/baz
+++ b/foo/bar/baz
@@ -23,4 +23,27 @@ 
+some 
+more 
+lines

Revert file to past version

Many more examples on undoing stuff here: Git examples: Resetting, Undoing and Reverting Changes

To revert a file to what it was like right after a commit, use git checkout <revision> -- path/to/file, and then run git commit. Local changes will be lost!

Example: Revert file foo/bar/baz to the way it was right after commit e7e5a8707df7746ecf

$ git checkout e7e5a8707df7746ecf -- foo/bar/baz

If you run git status now, you'll see the changes have been added but not committed:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   foo/bar/baz

Now commit the changes:

$ git commit -m "restore file to previous revision"
[master b262a6d] restore file to previous revision
 1 file changed, 23 deletions(-)