2

I am writing shell script to deploy a git branch from a remote repo.

This is the command I am using:

   git clone -q --depth=1 https://my.repourl.com/git-repo.git /my/destination/folder -b develop

The problem is, if the branch (develop in this case) is wrong, it just ignores and pulls from the master branch (?). I get this message:

  warning: Remote branch devel not found in upstream origin, using HEAD instead

I just want git to die/exit, if it does not find the branch specified. Any flags for that? Or any alternatives? git-archive did not work for some reason.

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

2 Answers2

1

As twalberg comments, git ls-remote --heads https://my.repourl.com/git-repo.git is the command to use for checking if a branch exists on the remote side.

The question "How to check if remote branch exists on a given remote repository?" lists the other possibility:

git clone -n
git fetch
# parse git branch -r

The test (bash) can look like:

br=$(git ls-remote --heads https://my.repourl.com/git-repo.git|grep abranch)
if [[ "${br}" != "" ]]; then
  git clone -b aBranch ...
fi
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Well, I have to do this in Shell Script and check the status of each command (success/fail). But the question actually how NOT to ignore warnings when branch does not exist when cloning? – Kevin Rave Mar 13 '13 at 20:01
  • 2
    @KevinRave and the answer is to *not* clone because you have detected *before* that the branch doesn't exist. – VonC Mar 13 '13 at 20:32
  • I got your point. So this is going to be done in two steps. Check if repo and branch exist and then do a clone, if branch exists. I thought if I could do this in one shot. :-) – Kevin Rave Mar 13 '13 at 20:46
  • BTW, can you elaborate your answer and write the `git` commands completely, so that it is useful to others? – Kevin Rave Mar 13 '13 at 20:48
0

I'm getting the same behavior as posted by Kevin with git v1.7.1 - but when testing with git v2.12.0, the clone command does actually fail when a non-existing branch is specified :

$ git clone --depth 1 -b FakeBranch --bare gitserver:/repo.git
Cloning into bare repository 'repo.git'...
warning: Could not find remote branch FakeBranch to clone.
fatal: Remote branch FakeBranch not found in upstream origin
user1564286
  • 195
  • 1
  • 8
  • It seems to be the case since 1.7.10: https://github.com/git/git/commit/920b691fe4da8115f9b79901411c0cc5fff17efe – VonC Nov 23 '18 at 18:09