Home > Blockchain >  GIT: How do I properly download a remote branch to local?
GIT: How do I properly download a remote branch to local?

Time:03-11

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.

CodePudding user response:

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

  • Related