有人推了一个分行testgit push origin test共享仓库的共享仓库。git branch -r如何查看遥控器test我试过了

  • git checkout test,它没有做任何事情
  • git checkout origin/test给给* (no branch)

当前回答

接受接受的答复不为你工作?

虽然第一个和选定的答案是技术上的正确中,您可能尚未从远程仓库中检索到所有对象和参考文献。如果是这种情况,您将会收到以下错误:

$ git checkout -b remote_branch origin/remote_branch

致命: git 检出: 更新路径与切换分支不兼容 。
您是否打算检查“ 来源/ remote_ branch ” 无法以承诺方式解决的“ 来源/ 远程- branch ” ?

解决方案

如果您收到此消息, 您必须首先完成git fetch origin何 地origin是运行前的远程仓库的名称git checkout remote_branch。以下是答复的完整例子:

$ git fetch origin
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 69 (delta 36), reused 66 (delta 33)
Unpacking objects: 100% (69/69), done.
From https://github.com/githubuser/repo-name
   e6ef1e0..5029161  develop    -> origin/develop
 * [new branch]      demo       -> origin/demo
   d80f8d7..359eab0  master     -> origin/master

$ git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'

如你所见 跑着跑着git fetch origin检索到我们尚未安装的远程分支, 以跟踪本地机器 。 从那里, 因为我们现在有一个 ref 到远程分支, 我们就可以运行git checkout remote_branch我们会从远程追踪中受益

其他回答

如果远程分支名称是以特殊字符开头的,您需要在退出命令中使用单引号,否则Git将不知道您在谈论哪个分支。

例如,我试图查看一个远程分支 命名为#9773,但如下图所示,命令没有正常工作:

Enter image description here

出于某种原因,我想知道尖锐符号(###)是否与它有关,然后我试图用单引号绕绕分支名称,例如'#9773'而不是仅仅#9773幸运的是,它的工作很好。

git checkout -b '#9773' origin/'#9773'

git branch -r表示对象名称无效, 因为该分支名称不在 Git 的本地分支列表中。 更新您的本地分支列表来源 :

git remote update

然后再试着检查一下你的远程分支

这对我管用

我相信git fetch拉拉进全部( 全部)远端树枝,这不是最初的海报所要的。

TL; DR TR; TL; TDR

使用git switch而不是git checkout。更多详情见此页面上的.

我认为答案是过时的。checkoutswitchrestore现在。

以下是我的总结:

如果您想要为远程分支更新一些内容, 您应该创建一个本地分支以“ 跟踪” 远程分支 。 您可以在本地更新任何您想要更新的内容, 并最终推到远程 。 如果您在克隆您的仓库后直接检查到远程分支, 您可能会看到“ 忽略的 HEAD” 状态, 以及 Git 的下列消息 :

Note: switching to 'origin/asd'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d3e1083 Update a

那么,我们如何建立本地分支来追踪远程分支呢?

要创建本地分支以跟踪远程分支, 您可以使用git checkout <remote branch name>git switch <remote branch name>。如果您有一个文件或文件夹的名称与您的远程分支名称相同,git checkout将输出一些错误消息, 但是git switch可以正常工作! 正常工作! 正常工作!

示例:

  1. 查看所有分支, 我们想要创建一个本地分支, 跟踪远程分支remotes/origin/asd,我们也有文件名asd:

    $ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/asd
      remotes/origin/ereres
      remotes/origin/master
      remotes/origin/zxc
    $ ls
    a  asd
    
  2. 文件名与远程分支相同, 如果我们使用 Git , Git 应该输出一些错误信息git checkout创建本地分支以跟踪远程分支

    $ git checkout asd
    fatal: 'asd' could be both a local file and a tracking branch.
    Please use -- (and optionally --no-guess) to disambiguate
    
  3. 如果我们用它,它就会有效git switch!

    $ git switch ereres
    Branch 'ereres' set up to track remote branch 'ereres' from 'origin'.
    Switched to a new branch 'ereres'
    
    $ git branch -vv
    * ereres 3895036 [origin/ereres] Update a
      master f9e24a9 [origin/master] Merge branch 'master' of
    

出于某种原因,我不能做:

git checkout -b branch-name origin/branch-name

它在抛出错误:

致命 : “ 来源/ 分支名称” 不是一个承诺, 无法从中创建分支“ 分支名称 ” 。

我必须这样做:

git checkout -b branch-name commit-sha

对我们来说,似乎remote.origin.fetch配置存在一个问题。 因此, 我们无法看到比master, 如此git fetch [--all]没有帮助,也没有帮助。git checkout mybranchgit checkout -b mybranch --track origin/mybranch确实工作过,虽然肯定在遥远的地方。

仅允许上一个配置master要获取的 :

$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master

通过使用来修补它*从源头获取新信息 :

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

$ git fetch
...
 * [new branch] ...
...

现在我们可以git checkout本地边远分支。

我不知道这个配置怎么会 出现在我们本地的仓库里