git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How to display "HEAD~*" in "git log"

From
Jeff King <peff@peff.net>
Date
Jul 6, 2022, 14:38 UTC
Message-ID
<YsWebbaOJbaOZ6i7@coredump.intra.peff.net>
In-Reply-To
<20220705092514.hsm7cou5bqvajvgq@carbon>
On Tue, Jul 05, 2022 at 12:25:14PM +0300, Konstantin Khomoutov wrote:
Show 17 quoted lines
> > because SHA sum is hard to memorize, so I have to use copy and past,
> > which is too boring. So, I wonder if there is a way to let "git log"
> > display commits like following:
> > 
> >   HEAD   <one line commit message>
> >   HEAD~1 <one line commit message>
> > HEAD~2 <one line commit message>
> > HEAD~3 <one line commit message>
> >   ...
> > 
> > With these "HEAD~*", I can easily directly type them and no need to
> > move my fingers out of keyboard.
> 
> You can script this. Provided you have a POSIX-compatible shell (such as
> Bash), the encantation would read something like
> 
>  $ git log --oneline | { n=0; while read line; do printf '%d\t%s\n' $n "$line"; done; }

That will just number the commits linearly as they are printed. If your history has any branches or merges, eventually it will get out of sync.

You can use "name-rev" to annotate commits with names that respect the history. It only matches full oids, so try:

  git log --oneline --no-abbrev | git name-rev --stdin

If you want shorter hex oids, you can work around it with "--name-only" and a custom format:

  git log --format='%h (%H) %s' | git name-rev --stdin --name-only

Note that the names will be based on the nearest branches/tags. You can use "--refs" to limit it, but sadly there doesn't seem to be a way to specify just HEAD. That might be a fun and easy feature to add.

And finally, if you really like this, you can configure git-log's pager to always pipe through name-rev, like this:

  [pager]
        log = "git name-rev --stdin | less"

Then the usual "medium" output from git-log will have the annotations on the "commit" lines. Arguably git-log ought to be able to do this internally, but I don't think anybody has ever implemented it (and I wouldn't be surprised if it's a little challenging, just because you'd have two traversals going on at once in the same program).

Note that I don't use any of those myself. Long ago I taught my terminal to do keyboard selection of object ids for cut-and-paste. I think there are probably solutions for tmux and other programs you can find online. I shared mine for urxvt a while ago:

  https://lore.kernel.org/git/20170120192539.7jts6xqzx46unn7y@sigill.intra.peff.net/

though if anybody is interested in it, let me know because I can share a new version with some bug fixes and improvements since then.

-Peff
Previous: kostix@bswap.ruNext: Konstantin Khomoutov
Message 6 of 11 in “How to display "HEAD~*" in "git log"”
  1. wuzhouhuiJul 5, 2022
  2. Konstantin KhomoutovJul 5, 2022
  3. Konstantin KhomoutovJul 5, 2022
  4. wuzhouhuiJul 5, 2022
  5. kostix@bswap.ruJul 6, 2022
  6. Jeff KingJul 6, 2022
  7. Konstantin KhomoutovJul 6, 2022
  8. Jeff KingJul 6, 2022
  9. Konstantin KhomoutovJul 6, 2022
  10. Jeff KingJul 7, 2022
  11. Junio C HamanoJul 6, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.