2

Want to download another remote branch to make maintenace, then push back.

so I tried:

git switch another_branch
fatal: invalid reference: another_branch

tried:

git checkout --track origin/another_branch
fatal: 'origin/another_branch' is not a commit and a branch 'another_branch' cannot be created from it

tried:

git checkout another_branch
error: pathspec 'another_branch' did not match any file(s) known to git

tried:

git fetch origin another_branch:another_branch
From gitlab.my-inc.com:projname/proj
* [new branch]      another_branch   -> another_branch
git checkout another_branch
Switched to branch 'another_branch'

seems good but:

git pull
fatal: refusing to merge unrelated histories

git branch -vv
* another_branch            421f45e normal commit comments

git branch --set-upstream-to origin/another_branch
error: the requested upstream branch 'origin/another_branch' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

so, I tried:

git fetch
git fetch origin another_branch
From gitlab.my-inc.com:projname/proj
* branch            another_branch   -> FETCH_HEAD

and still got:

git branch --set-upstream-to origin/another_branch
error: the requested upstream branch 'origin/another_branch' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

Now I feel totally not human seeing others playing well with their git...

So please help, and thanks in advance.

6
  • 1
    Did you run git fetch origin? It seems you only specified the remote but this does not fetch said remote. Mar 8, 2022 at 7:06
  • @SimonDoppler Thank you for the advice. Tried just now: git fetch origin (blank output), then git branch --set-upstream-to xxx -> branch does not exsit , and git pull -> refusing to merge unrelated histories.
    – Chiron
    Mar 8, 2022 at 7:12
  • what do you see when you run git branch -a | grep "another_branch" and git branch -a ? as suggested by VonC : you may have a refspec which maps remote branches to a non standard name.
    – LeGEC
    Mar 8, 2022 at 8:12
  • 1
    You probably made a single-branch clone: check the remote.origin.fetch setting, as VonC suggests.
    – torek
    Mar 10, 2022 at 0:24
  • @LeGEC grep returns blank result for "another_branch". seems to be the problem.
    – Chiron
    Mar 11, 2022 at 9:11

1 Answer 1

3

As long as git branch -avv does not list origin/another_branch, a simple git switch another_branch will not work.

Its guess mode would make that command as:

If <branch> is not found, but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to:

$ git switch -c <branch> --track <remote>/<branch>

In your case, check the refspec of your remote

git config remote.origin.fetch

Or, as torek suggests in the comments, in case of multi-values:

git config --get-all remote.origin.fetch

It should be like

fetch = +refs/heads/*:refs/remotes/origin/*

Which means it should fetch all branches, instead of fetching only one branch, for instance:

+refs/heads/main:refs/remotes/origin/main

If it does not fetch all branches, set it using that refspec (+refs/heads/*:refs/remotes/origin/*), and a git fetch will be able to show you remote branches of the remote repository, including, hopefully, origin/another_branch

3
  • 1
    Minor: better to run git config --get-all remote.origin.fetch in case it is multi-valued. But I'm betting the OP has a single-branch clone, with +refs/heads/main:refs/remotes/origin/main or similar, as the only value.
    – torek
    Mar 10, 2022 at 0:25
  • 1
    @torek Thank you. I have included your comment in the answer for more visibility.
    – VonC
    Mar 10, 2022 at 7:31
  • wildcard works like a charm. now every branch is defined.
    – Chiron
    Mar 11, 2022 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.