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

Stephan Bergmann sbergman at
Fri Sep 15 06:54:52 UTC 2017


> 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?

More information about the LibreOffice mailing list