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

Best way to update `HEAD` in mirrored repos

From
RMRodrigo Silva Mendoza <rodrigosilvamendoza3@gmail.com>
Date
Jun 8, 2022, 20:36 UTC
Message-ID
<CANWRddN4R6AceeaOyZm1vs8AXBNv3J+cE5MOyrhKVhcqddjUOA@mail.gmail.com>
Hi all,

Given a repo cloned like so: `git clone --mirror someRemote`

And then with its origin updated like so `git remote set-url origin someRemote`

What would be the best way to update the `HEAD` ref in the clone, if some change happens to the remote HEAD? Like say, the default branch changing from `main` to `dev` or being renamed from `main` to `main_2`.

What I've got that I think works
1. Get the ref from the origin that points to `HEAD`. Extract the ref
from the output.
2. Manually update the `HEAD` ref with the extracted output prior step

Like so: ``` $ git ls-remote --symref origin HEAD ref: refs/heads/good_main_3 HEAD 0666a519f94b8500ab6f14bdf7c9c2e5ca7d5821 HEAD

$ git symbolic-ref HEAD refs/heads/good_main_3 ```

Does this make sense?
The following are some of the things I've tried - they all fail to
update the `HEAD` file, or fail for some other reason.
`git fetch -p`
`git fetch -p -a`
`get fetch -p -a -u`
`git remote set-head origin -a`
   Fails with "error: Not a valid ref:
refs/remotes/origin/good_main_3". The ref it fails with is whatever
the remote HEAD is.
`git remote update --prune`

And my use case: I have a set of git repos cloned/mirrored. They are then indexed for search, using the `HEAD` ref. The indexer runs periodically, and before indexing it always runs `get fetch -p` to update repos. For each repo, to get the latest commit to index, I use the libgit2 equivalent of `git rev-parse HEAD^0`. Right now, previously cloned repos whose HEAD is updated after the initial clone will fail at this step, which causes them to not get indexed. Here's an example run, all for the same repo `x`.

1. `git clone & set origin for repo x` (indexer first clones repo if
it doesn't exist)
2. `git rev-parse HEAD^0` - successfully outputs some commit sha
3. Github default branch is renamed from `main` to `main_3`
4. `git fetch -p` - indexer runs this to update
5. `git rev-parse HEAD^0` - now outputs "fatal: ambiguous argument
'HEAD^0': unknown revision or path not in the working tree."
Thanks for your time,
Rodrigo
Next: Junio C Hamano
Message 1 of 9 in “Best way to update `HEAD` in mirrored repos”
  1. Rodrigo Silva MendozaJun 8, 2022
  2. Junio C HamanoJun 8, 2022
  3. Rodrigo Silva MendozaJun 9, 2022
  4. Johannes SchindelinJun 9, 2022
  5. Rodrigo Silva MendozaJun 9, 2022
  6. Johannes SchindelinJun 10, 2022
  7. Junio C HamanoJun 10, 2022
  8. Rodrigo Silva MendozaJun 15, 2022
  9. Rodrigo Silva MendozaJun 15, 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.