Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show sha1 only with git log

Tags:

git

bash

I want to write a Bash script that loops over the sha1s of commits output by an invocation of git log. However, git log gives me much more output than I want:

commit 0375602ba2017ba8750a58e934b41153faee6fcb
Author: Mark Amery <[email protected]>
Date:   Wed Jan 1 21:35:07 2014 +0000

    Yet another commit message

    This one even has newlines.

commit 4390ee9f4428c84bdbeb2fed0a461099a6c81b39
Author: Mark Amery <[email protected]>
Date:   Wed Jan 1 21:30:19 2014 +0000

    Second commit message.

commit bff53bfbc56485c4c1007b0884bb1c0d61a1cf71
Author: Mark Amery <[email protected]>
Date:   Wed Jan 1 21:28:27 2014 +0000

    First commit message.

How can I get git log to just output sha1s so that I can loop over them conveniently?

like image 887
Mark Amery Avatar asked Jan 01 '14 21:01

Mark Amery


People also ask

What information does a git log show?

The git log command shows a list of all the commits made to a repository. You can see the hash of each Git commit , the message associated with each commit, and more metadata. This command is useful for displaying the history of a repository.

What is git log -- Oneline?

Git Log OnelineThe oneline option is used to display the output as one commit per line. It also shows the output in brief like the first seven characters of the commit SHA and the commit message. It will be used as follows: $ git log --oneline.

How do I access git logs?

The command for that would be git log -n where n represents the number up to which commit you to want to see the logs.


1 Answers

You can use the --format argument with a custom format that only includes the sha1:

git log --format=format:%H

The above command yields output like the following:

0375602ba2017ba8750a58e934b41153faee6fcb
4390ee9f4428c84bdbeb2fed0a461099a6c81b39
bff53bfbc56485c4c1007b0884bb1c0d61a1cf71

You can loop over the commit hashes in Bash like this:

for sha1 in $(git log --format=format:%H); do
    : # Do something with $sha1
done

This is slightly more verbose than using git rev-list, but may be your only option if you want to use ordering or filtering arguments for git log that are not supported by git rev-list, like -S.

like image 198
Mark Amery Avatar answered Sep 30 '22 11:09

Mark Amery