How do I make `origin/HEAD` work with partial fetch settings?


In a typical "regular" git clone, one ends up with .git/config as follows:

[remote "origin"]
    url = [email protected]:orgname/reponame.git
    fetch = +refs/heads/*:refs/remotes/origin/*     

and then git show origin/HEAD works fine.

However, I don’t want all branches to be fetched, only prod and ones prefixed with jakub/, so my .git/config is as follows:

[remote "origin"]
    url = [email protected]:orgname/reponame.git
    fetch = +refs/heads/prod:refs/remotes/origin/prod
    fetch = +refs/heads/jakub/*:refs/remotes/origin/jakub/*

(I didn’t do a git clone, but created a .git/config like above, and then did git fetch).

However, with that setup, git show origin/HEAD no longer works:

fatal: ambiguous argument 'origin/HEAD': unknown revision or path not in the working tree.

I tried to add the following to .git/config but it doesn’t work:

    fetch = +refs/heads/HEAD:refs/remotes/origin/HEAD
$ git fetch
fatal: couldn't find remote ref refs/heads/HEAD

What’s the proper way to make origin/HEAD work with partial fetching?


If the HEAD branch of the remote repository is one of the branches configured in your fetch refspecs, then this should work:

git remote set-head origin -a

This will query the remote for its HEAD and set origin/HEAD to point to the corresponding remote-tracking branch in your repository.

If the remote HEAD points to a branch thst is not covered by your fetch refspec, then you would have to add a fetch refspec for that branch, fetch it, and then run the above command.

Answered By – philb

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published