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

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

From
KKKonstantin Khomoutov <kostix@bswap.ru>
Date
Jul 5, 2022, 09:25 UTC
Message-ID
<20220705092514.hsm7cou5bqvajvgq@carbon>
In-Reply-To
<17411d88-b27a-2d20-623d-85c49dc7754e@mails.ucas.ac.cn>
On Tue, Jul 05, 2022 at 03:11:49PM +0800, wuzhouhui wrote:
Show 19 quoted lines
> I frequently use "git rebase" to move commit to a specific location, to
> know which commit ID as param of "git rebase", I have to list all commits
> by "git log --oneline", then copy specific commit ID, and executing "git
> rebase" as
> 
>   git rebase -i <copied commit ID>~
> 
> 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; }
This is admittedly not convenient so it makes sense to turn into an alias:
 [alias]
   relog = "!relog() { git log --oneline \"$@\" | { n=0; while read line; do printf 'HEAD~%d\t%s\n' $n \"$line\"; n=$((n+1)); done; }; }; relog"
Then the call
 $ git relog
would output something like

HEAD~0 deadbeef Commit message HEAD~1 fadedfac Another commit message HEAD~2 12345678 Yet another commit message

...and so on.

It's easy to make that script not output "~0" for the very first entry and output just "~" instead of "~1" for the second, but I what's presented is enough for an example.

Previous: wuzhouhuiNext: Konstantin Khomoutov
Message 2 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.