Search code examples
gitgit-checkoutgit-remote

Why won't Git let me check out a branch from an alternate remote?


I'm working on a project that has two remotes, one called origin and one called vsts. The default remote is origin. Here's the output from git remote -v, with some parts anonymized as ***:

$ git remote -v origin [email protected]:***/***.git (fetch) origin [email protected]:***/***.git (push) vsts ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** (fetch) vsts ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** (push)

I'm trying to check out a new branch from vsts. It's called release/1.4.1. I'm on Git 2.16.x, so I should be able to use git checkout, but this is what happens:

$ git checkout release/1.4.1 error: pathspec 'release/1.4.1' did not match any file(s) known to git.

I think maybe it's assuming I meant origin. So I try this:

$ git checkout vsts/release/1.4.1 error: pathspec 'vsts/release/1.4.1' did not match any file(s) known to git.

I should make sure git can find the branch. So I use git ls-remote to get a list of remote branches:

$ git ls-remote vsts ... abcde*** refs/heads/release/1.4.1 ...

I get a list of branches and commit hashes, and release/1.4.1 is definitely one of them.

I try a few more things:

$ git checkout -b release/1.4.1 vsts/release/1.4.1 fatal: 'vsts/release/1.4.1' is not a commit and a branch 'release/1.4.1' cannot be created from it

$ git fetch vsts release/1.4.1 From ssh://vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** * branch release/1.4.1 -> FETCH_HEAD

(After this command, I try all the previous ones again. The results are unchanged.)

$ git checkout -b release/1.4.1 remotes/vsts/release/1.4.1 fatal: 'remotes/vsts/release/1.4.1' is not a commit and a branch 'release/1.4.1' cannot be created from it

$ git checkout -b release/1.4.1 remotes/vsts/refs/heads/release/1.4.1 fatal: 'remotes/vsts/refs/heads/release/1.4.1' is not a commit and a branch 'release/1.4.1' cannot be created from it

If I try git pull vsts/release/1.4.1 it successfully merges the remote branch release/1.4.1 into the current branch, but that's not a useful workaround here.

What else can I try? I don't understand why I can't check out the remote branch.


Solution

  • The problem was that my local git config was messed up. I used git config --local -e to open it in vim and found this section:

    [remote "vsts"] url = ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** fetch = +refs/heads/dev:refs/remotes/vsts/dev

    Which looks like it's only set up to fetch dev. I don't know how it got that way, but I changed it to the following:

    [remote "vsts"] url = ssh://***@vs-ssh.visualstudio.com:***/DefaultCollection/***/_ssh/*** fetch = +refs/heads/*:refs/remotes/vsts/*

    After which I'm able to do this:

    $ git checkout release/1.4.1 Switched to a new branch 'release/1.4.1' Branch 'release/1.4.1' set up to track remote branch 'release/1.4.1' from 'vsts'.