GIT: 如何正确下载远程分支到本地?

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

想下载另一个远程分支进行维护,然后推回去。

所以我尝试了:

git switch another_branch
fatal: invalid reference: another_branch

尝试过:

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

尝试过:

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

尝试过:

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'

看起来不错但是:

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.

所以,我试过了:

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

仍然得到:

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.

现在看到别人用他们的git...

玩得很好,我觉得完全不像人

所以请帮忙,提前致谢。

只要git branch -avv不列出origin/another_branch,一个简单的git switch another_branch就不行

guess mode 将使该命令为:

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>

对于您的情况,请检查 refspec of your remote

git config remote.origin.fetch

或者,如 torek suggests in ,在 multi-values 的情况下:

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

应该是

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

这意味着它应该获取所有个分支,而不是只获取一个分支,例如:

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

如果它没有获取所有分支,请使用该 refspec (+refs/heads/*:refs/remotes/origin/*) 设置它,git fetch 将能够向您显示远程存储库的远程分支,希望包括, origin/another_branch