2

I would like to check out the latest state of a remote git branch in a script for some integration-tests.

The branch name can be changed by the programmer in between runs. I would like git not to fully clone the whole repository each time in order not to lose too much time.

Since the script gets run regularly on an existing repo, the branch can already exist locally.

I have a hard time getting the commands right. The solution below fails to pull when a remote (unrelated branch) is deleted.

         if cd $builddir ;
            then git fetch origin $gitbranch &&  git checkout --track $gitbranch ; git pull
            else git clone --branch $gitbranch $gitrepo $builddir;
         fi

Is there an elegant solution for this? I am using git 1.7.1 on Centos 6.5.

mirk
  • 5,302
  • 3
  • 32
  • 49
  • What is the error message? – VonC Jun 30 '14 at 08:08
  • @VonC The error message is Your configuration specifies to rebase against the ref 'some_branch' from the remote, but no such ref was fetched. – mirk Jun 30 '14 at 08:10
  • Strange, considering your script isn't supposed to use '`some_branch`' (as you say: `some_branch` is an *unrelated* deleted branch) – VonC Jun 30 '14 at 08:11
  • However, I think that there something fundamentally wrong in my whole aproach. Maybe I can check out in a detached state or something? – mirk Jun 30 '14 at 08:12
  • No, the approach seems good, it is more the local config which references some unrelated branch, config which can becomes obsolete if said branch is deleted on the remote. – VonC Jun 30 '14 at 08:12
  • @VonC: 'some_branch' is probably still be checked out locally from a past run when getting the error-message. – mirk Jun 30 '14 at 08:13

2 Answers2

1

Considering an older instance of the script could have checked out a branch now deleted on the remote side, I would first remove any local config branch setting:

git config --remove-section branch.old_branch

(you can iterate on all local branches, with, for instance, a for-each)

Then, I would try to fetch/clone

That would avoid the error message:

Your configuration specifies to rebase against the ref '`old_branch`' from the remote, but no such ref was fetched.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

You can try to delete stale remote refs:

git remote prune origin
Kousha
  • 1,575
  • 14
  • 18