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

Re: How can I get a list of checkout history?

From
Jeff King <peff@peff.net>
Date
Feb 15, 2016, 16:56 UTC
Message-ID
<20160215165603.GD25364@sigill.intra.peff.net>
In-Reply-To
<CAHd499D5nMvFQ-eok_3tMZTNp+9JdcMZSL9XrXqkdUNxmRq_eQ@mail.gmail.com>
On Mon, Feb 15, 2016 at 09:18:50AM -0600, Robert Dailey wrote:
Show 15 quoted lines
> As you know, I can checkout the Nth checked out branch via this syntax:
> 
> $ git checkout @{-N}
> 
> Is there a built-in mechanism to get a listing of previously checked
> out refs? Basically, this would be similar to 'history' command in
> linux where instead of actual commands, it lists like this:
> 
> HEAD@{-1}: master
> HEAD@{-2}: topic1
> HEAD@{-3}: 3f556e9 (detached)
> 
> Seems like reflog should be able to do this, and maybe it can, but I'm
> not sure. Any tips? I'd be fine making a convenient alias for this if
> it ends up being a series of piped commands.

The "@{-N}" syntax works by reading the HEAD reflog backwards and grepping for "checkout: moving from ...". The implementation is in grab_nth_branch_switch.

You could do it yourself like:
  git reflog HEAD |
  perl -lne '/checkout: moving from (\S+) to/ and print $1'

That includes detached HEADs, too. If you want just "real" branaches, you could possibly omit entries which match [0-9a-f]{40}. But that's just a heuristic (you _could_ have a branch that matches that).

-Peff
Previous: Robert Dailey
Message 2 of 2 in “How can I get a list of checkout history?”
  1. Robert DaileyFeb 15, 2016
  2. Jeff KingFeb 15, 2016

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.