Git help needed: update local repo to specific branch or tag

Jan-Marek Glogowski glogow at
Fri Sep 15 14:20:18 UTC 2017

Am 15.09.2017 um 08:54 schrieb Stephan Bergmann:
> At
> <;a=blob;f=flatpak/;h=fab02d7f71f38c1f30174a534627db2bcc211ebd;hb=HEAD#l50>,
>> if [ -e "${my_dir?}"/lo ]; then
>>  git -C "${my_dir?}"/lo fetch --tags
>>  git -C "${my_dir?}"/lo checkout "${my_gitbranch?}"
>> else
>>  git clone --branch "${my_gitbranch?}"
>> git:// \
>>   "${my_dir?}"/lo
>> fi
> I want to get a local checkout of $my_gitbranch (which will either
> denote a branch like libreoffice-5-4 or a tag like libreoffice-
> of our LO repo.  The "else" case is straightforward.
> But if I already have the repo checked out from a previous run of the
> script (with a potentially different value for $my_gitbranch), I don't
> want to clone again, but re-use the existing repo and just update it
> accordingly (the "then" branch).  The sequence of git commands works
> fine if $my_gitbranch is a tag.
> However, if $my_gitbranch is a branch, and the existing repo happens to
> already have that branch checked out, then the 'git checkout' will
> report that I'm behind the remote repo by N commits and should
> fast-forward.
> So adding a 'git pull' after the 'git checkout' would help the case
> where $my_gitbranch is a branch.  But when it is a tag, such a 'git
> pull' would fail, stating that I'm not on a branch.
> Is there a magic git incantation that does what I want?

Use detached heads and don't use local branches at all. Untested, but
should work:

if [ ... ]; then
  git fetch --prune --tags origin
  # cleanup all patched stuff - probably not needed
  git reset --hard
  git checkout --detached "origin/${my_gitbranch?}"
  git clone --no-checkout git://
  git checkout --detached "origin/${my_gitbranch?}"

Alternatively to checkouts you can just reset your local branch to your
preferred state using:

git reset --hard "origin/${my_gitbranch?}"

Just remember that at this point the branch is "fake".



More information about the LibreOffice mailing list