Returning a git log value for use in a script

By default, git log output is displayed in less. But what if we want to use that value in a script? You can add the --no-pager flag as part of the command:

git --no-pager log -1 --pretty='%B' will return the last commit message.

git --no-pager log -1 --pretty='%cn' will return the committer.

So you can now use these return values in scripts. We use these to assign blame when someone breaks the build:


COMMITTER=$(git --no-pager log -1 --pretty='%cn')
echo $COMMITTER broke the build!

Leave a Reply