Show a git log with offsets relative to HEAD

git log | nl -w9 -v0 --body-numbering='pcommit\ [0-9a-f]\{40\}' | sed 's/^ \+\([0-9]\+\)\s\+/HEAD~\1 /'
This is how you can do this without having to use oneline
Sample Output
HEAD~0 commit aa22191093e5becfa8167e4961b617f15a81894a
          Merge: 9687008 3aa70c1
          Author: David Calavera <>
          Date:   Mon Nov 23 12:43:04 2015 -0800
              Merge pull request #17356 from HuKeping/warn-on-oom
              Warning out when disalbe oom killer but not set the memory limit
HEAD~1 commit 968700837890c2a5d1dc41aa871f3c94aca07640
          Merge: 05d1a5a 6f6f10a
          Author: Alexander Morozov <>
          Date:   Mon Nov 23 10:38:45 2015 -0800
              Merge pull request #18104 from hypriot/17802-remove-journald-to-fix-dynbinary-build-on-ARM
              remove journald to fix dynbinary build on ARM
HEAD~2 commit 05d1a5aed6ba27ba5f03ba798beb4fb4f11de9e2
          Merge: 804d2f7 ca94adc
          Author: Tianon Gravi <>
          Date:   Mon Nov 23 10:35:15 2015 -0800
              Merge pull request #18152 from rgl/master
              mkimage-arch: remove the test container after run
HEAD~3 commit 804d2f73c8996600b3d57c69267c3131da2d3b37
          Merge: 86377e5 463c53c
          Author: Alexander Morozov <>
          Date:   Mon Nov 23 10:21:52 2015 -0800
              Merge pull request #18080 from aditirajagopal/16756-docker_api_images_test
              Checkers on integration-cli/docker_api_images_test
HEAD~4 commit 86377e541db6acb8a3a175b9e4805e5a08792e0d
          Merge: 097bc48 bb50f25
          Author: Sebastiaan van Stijn <>
          Date:   Mon Nov 23 18:42:38 2015 +0100
              Merge pull request #18157 from Djelibeybi/master

2015-11-23 21:53:33

2 Alternatives + Submit Alt

  • Print a git log (in reverse order) giving a reference relative to HEAD. HEAD (the current revision) can also be referred to as HEAD~0 The previous revision is HEAD~1 then HEAD~2 etc. . Add line numbers to the git output, starting at zero: ... | nl -v0 | ... . Insert the string 'HEAD~' before the number using sed: ... | sed 's/^ \+/&HEAD~/' . Thanks to bartonski for the idea :-) Show Sample Output

    git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
    flatcap · 2015-11-23 21:35:57 0

  • 1
    o=0; git log --oneline | while read l; do printf "%+9s %s\n" "HEAD~${o}" "$l"; o=$(($o+1)); done | less
    bartonski · 2015-11-23 17:47:16 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: