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

发布于 2025-01-12 10:43:39 字数 2117 浏览 3 评论 0原文

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

所以我尝试了:

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上玩得很好......

所以请帮忙,并提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2025-01-19 10:43:39

只要 gitbranch -avv 不列出origin/another_branch,简单的 git switch another_branch 就不起作用。

它的 猜测模式 将使命令为:

如果未找到 ,但在一个远程(称为 )中确实存在具有匹配名称的跟踪分支,视为等同于:

$ git switch -c <分支>; --track <远程>/<分支>

根据您的情况,请检查遥控器的refspec

git config remote.origin.fetch

或者,正如 torekcomments,在多值的情况下:

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 将能够向您显示远程存储库,希望包括, 起源/another_branch

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文