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

Restoring detached HEADs after Git operations

From
Patrick Lehmann <patrick.lehmann@plc2.de>
Date
Jun 19, 2017, 08:46 UTC
Message-ID
<0092CDD27C5F9D418B0F3E9B5D05BE08010287DF@SBS2011.opfingen.plc2.de>
Hello,
I wrote a Bash script to recover branch names after Git operations have create detached HEADs in a Git repository containing lots of Git submodules. The script works recursively.

I would like to see: a) that script or algorithm being integrated into Git by default b) that as a default behavior for all Git operations creating detached HEADs

That's the command: -------------------------------- git submodule foreach --recursive 'HEAD=$(git branch --list | head -n 1); if [[ "$HEAD" == *HEAD* ]]; then REF=$(git rev-parse HEAD); FOUND=0; 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; if [[ $FOUND -eq 0 ]]; then echo -e " \e[31mNo matching branch found.\e[0m"; fi else echo -e " \e[36mNothing to do.\e[0m"; fi' --------------------------------

How does it work:
1. It uses git submodule foreach to dive into each Git submodule and execute a series of Bash commands.
2. It's reading the list of branches and checks if the submodule is in detached mode. The first line contains the string HEAD.
3. Retrieve the hash of the detached HEAD
4. Iterate all local branches and get their hashes
5. Compare the branch hashes with the detached HEAD's hash. If it matches do a checkout.
6. Report if no branch name was found or if a HEAD was not in detached mode.
The Bash code with line breaks and indentation:
--------------------------------
HEAD=$(git branch --list | head -n 1)
if [[ "$HEAD" == *HEAD* ]]; then
  REF=$(git rev-parse HEAD)
  FOUND=0
  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
  if [[ $FOUND -eq 0 ]]; then
    echo -e "  \e[31mNo matching branch found.\e[0m"
  fi
else
  echo -e "  \e[36mNothing to do.\e[0m"
fi
--------------------------------
Are their any chances to get it integrated into Git?
I tried to register that code as a Git alias, but git config complains about quote problem not showing where. It neither specifies if it's a single or double quote problem. Any advice on how to register that piece of code as an alias?
If wished, I think I could expand the script to also recover hash values to Git tags if no branch was found.
Kind regards
    Patrick Lehmann
Next: Lars Schneider
Message 1 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.