During a pre-commit
hook (actually a ref-update
in Gerrit) the repository is in a detached HEAD mode (witch is fine).
Doing a git clone path/to/my/repo
does clone/fetch this commit, but I'd like to just do a git fetch
and avoid cloning my repository each time there is a commit (for various reasons like avoiding to change file time stamps each time).
Doing a git fetch --all
on a previously cloned repository (like above) does not fetch this detached head commit. Note that I do have the commit's SHA1 but a git checkout commit-sha1-here
doesn't work either since it hasn't been fetched.
I do know it's by design that only remote branches are fetched, but since clone can fetch detached heads, isn't there a way to update my cloned repository to retrieve them later? I'd like to avoid having to create a temporary branch each time just for that.
The final goal is to have an updated work-tree of a bare repository (detached HEAD).
Answering my own question here.
git pull
of a detached HEAD seems not allowed by design but a git push
of that detached HEAD is possible (i.e., instead of fetching from the receiving side you push from the sending side):
$ git push <repository> +<sha1>:refs/remotes/origin/master
Then from the repository receiving it:
$ git checkout <sha1> # same as origin/master
Note: Pushing to refs/remotes/origin/master
instead of pushing directly to master
is disallowed by default if it's the current branch or a non-base repository, so we push to the origin reference.