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

Re: Restoring detached HEADs after Git operations

From
Jeff King <peff@peff.net>
Date
Jun 19, 2017, 17:01 UTC
Message-ID
<20170619170124.6oj6stoojds4srci@sigill.intra.peff.net>
In-Reply-To
<0092CDD27C5F9D418B0F3E9B5D05BE08010287DF@SBS2011.opfingen.plc2.de>
On Mon, Jun 19, 2017 at 08:46:45AM +0000, Patrick Lehmann wrote:
Show 8 quoted lines
>   for Branch in $(git branch --list | grep "^  " | sed -e "s/  //" ); do
>     if [[ "$(git rev-parse "$Branch")" == $REF ]]; then
>       echo -e "  \e[36mCheckout $Branch...\e[0m"
>       git checkout $Branch
>       FOUND=1
>       break
>     fi
>   done

I see Lars pointed you at for-each-ref, which is the preferred way for scripts to enumerate branches. But you can also use --points-at to skip the inner part of your loop entirely, like:

  git for-each-ref --points-at=HEAD --format='%(refname:short)' refs/heads

That should run much faster, as it only has to spawn one process rather than one for each branch (it will print all of them, of course. You can pipe through head -n 1 to get the first, and check out --sort if you want to prioritize by recency or similar).

-Peff
Previous: Stefan BellerNext: Ævar Arnfjörð Bjarmason
Message 13 of 14 in “Restoring detached HEADs after Git operations”
  1. Patrick LehmannJun 19, 2017
  2. Lars SchneiderJun 19, 2017
  3. AW: Restoring detached HEADs after Git operationsPatrick Lehmann, Jun 19, 2017
  4. Stefan BellerJun 19, 2017
  5. AW: Restoring detached HEADs after Git operationsPatrick Lehmann, Jun 19, 2017
  6. Stefan BellerJun 19, 2017
  7. AW: Restoring detached HEADs after Git operationsPatrick Lehmann, Jun 19, 2017
  8. Stefan BellerJun 19, 2017
  9. AW: Restoring detached HEADs after Git operationsPatrick Lehmann, Jun 19, 2017
  10. Junio C HamanoJun 19, 2017
  11. Stefan BellerJun 19, 2017
  12. Stefan BellerJun 19, 2017
  13. Jeff KingJun 19, 2017
  14. Ævar Arnfjörð BjarmasonJun 19, 2017

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.