34

How to get the author of the latest commit in a Git repository?

#!/bin/bash

git_log=`git ls-remote git url  master`
git_commitId = git_log | cut -d " " -f1
echo $git_commitId

cd /workspace
git_log_verify = `git rev-parse HEAD`
echo $git_log_verify

if $git_commitId =$git_log_verify then
    cd /workspace
    git_authorName=`git log --pretty=format:"%an"`;
    echo $git_authorName
fi
ib.
  • 27,830
  • 11
  • 80
  • 100
Saurabh singhal
  • 379
  • 1
  • 3
  • 3

4 Answers4

112

This is what you're looking for:

git log -1 --pretty=format:'%an'
John Bupit
  • 10,406
  • 8
  • 39
  • 75
  • 7
    And if you need this for a script or variable, just add the `xargs` such as. `git log -1 --pretty=format:'%an' | xargs ` – aemonge May 13 '19 at 10:43
  • Where do you get all the escape codes from? – theonlygusti Jan 13 '22 at 01:19
  • @theonlygusti you can get them from this [git log#format](https://git-scm.com/docs/git-log#_pretty_formats) link (scroll down a little and you'll see them all) – ghiscoding Jul 08 '22 at 03:01
34

Or to retrieve the author's email, instead of name:

git log -1 --pretty=format:'%ae'
Donald Steffy
  • 341
  • 3
  • 2
19

To get author name:

git log -1 --pretty=format:'%an'

To get author email:

git log -1 --pretty=format:'%ae'
smmehrab
  • 756
  • 9
  • 16
3

how to assign to variable in Groovy:

def builtBy = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%an'").trim()
AKB
  • 5,918
  • 10
  • 53
  • 90
  • in jenkins declarative pipeline you have to double %%: (win) `def builtBy = bat (returnStdout: true, script: "git log -n 1 --pretty=format:'%%an'").trim() println buildBy` – Sasha Bond Dec 17 '22 at 01:55