首页 文章

Git获取远程分支

提问于
浏览
1721

我的同事和我在同一个存储库上工作,我们已经将它分成两个分支,每个分支在技术上用于不同的项目,但它们有相似之处,因此我们有时会想要从分支机构返回给* master .

但是,我有分支机构 . 我的问题是,我的同事怎么能专门拉那个分支?回购邮件的 git clone 似乎并没有为他本地创建分支,尽管我可以看到他们在推动我的结束之后生活在一团糟 .

此外,当我最初创建分支时,我做了 -b checkout . 不确定这是否有很大的不同?

iMac:test_solar dave$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/discover
origin/master

git fetch origin discover
git checkout discover

这些是我运行的命令 . 但它绝对不起作用 . 我希望能够检查该分支,然后推送并提交来自各个协作者或工作站的分支更改 .

25 回答

  • 779

    是什么帮助了我

    1)查看所有可用的远程分支(例如'remote-branch-name')

    git branch -r
    

    2)使用远程分支名称创建本地分支

    git fetch && git checkout 'remote-branch-name'
    
  • 7

    步骤如下;

    • git fetch origingit fetch --all ,这将获取到本地的所有远程分支,然后这是您可以使用的第二个选项 .

    • git checkout --track origin/<The_remote_branch you want to switch over>

    然后在这个分支上工作,你可以通过输入来验证你是否在那个分支上

    git branch
    

    它显示您当前所在的分支 .

  • 289

    假设你的遥控器是git@xyz.git,你想要它的random_branch分支 . 该过程应如下:

    • 首先检查您的遥控器列表

    git remote -v

    • 如果你在上面命令的输出中没有git@xyz.git remote,你可以添加它

    git remote add xyz git@xyz.git

    • 现在您可以通过获取该遥控器的内容

    git fetch xyz

    • 现在检查那个遥控器的分支

    git checkout -b my_copy_random_branch xyz / random_branch

    • 检查分支列表

    git branch -a

    本地分支my_copy_random_branch将跟踪远程的random_branch分支 .

  • 10

    如果您有一个使用--depth 1克隆的存储库,则列出的许多命令将不起作用 . 例如,请看这里

    % git clone --depth 1 https://github.com/repo/code
    Cloning into 'code'...
    cd code
    remote: Counting objects: 1778, done.
    remote: Compressing objects: 100% (1105/1105), done.
    remote: Total 1778 (delta 87), reused 1390 (delta 58), pack-reused 0
    Receiving objects: 100% (1778/1778), 5.54 MiB | 4.33 MiB/s, done.
    Resolving deltas: 100% (87/87), done.
    Checking connectivity... done.
    Checking out files: 100% (1215/1215), done.
    % cd code
    % git checkout other_branch
    error: pathspec 'other_branch' did not match any file(s) known to git.
    % git fetch origin other_branch
    remote: Counting objects: 47289, done.
    remote: Compressing objects: 100% (15906/15906), done.
    remote: Total 47289 (delta 30151), reused 46699 (delta 29570), pack-reused 0
    Receiving objects: 100% (47289/47289), 31.03 MiB | 5.70 MiB/s, done.
    Resolving deltas: 100% (30151/30151), completed with 362 local objects.
    From https://github.com/repo/code
     * branch            other_branch-> FETCH_HEAD
    % git checkout other_branch
    error: pathspec 'other_branch' did not match any file(s) known to git.
    %
    

    在这种情况下,我会重新回复回购,但也许还有其他技术,例如git shallow clone (clone --depth) misses remote branches

  • 3
    git checkout -b serverfix origin/serverfix
    

    这是一个很常见的操作,git提供了--track简写:

    git checkout --track origin/serverfix
    

    事实上,这种情况非常普遍,甚至还有一条捷径 . 如果您尝试签出的分支名称(a)不存在,并且(b)只与一个遥控器上的名称完全匹配,Git将为您创建跟踪分支:

    git checkout serverfix
    

    要设置名称与远程分支不同的本地分支,可以轻松地使用具有不同本地分支名称的第一个版本:

    git checkout -b sf origin/serverfix
    

    现在,您的本地分支sf将自动从origin / serverfix中提取 .

    来源:Pro Git 2nd Edition, written by Scott Chacon and Ben Straub(为便于阅读而削减)

  • 3

    如果你已经知道你的远程分支......

    git remote
    => One
    => Two
    

    而且你知道你想结账的分行名称 . br1.2.3.4 然后呢

    git fetch One
    => returns all meta data of remote i.e. the branch name in question.
    

    剩下的就是检查分支机构

    git checkout br.1.2.3.4
    

    然后从中创建任何新分支 .

  • 1

    一个简单的命令 - “git checkout remote_branch_name”将帮助您创建一个包含远程分支中所有更改的本地分支 .

  • 1

    您需要创建一个跟踪远程分支的本地分支 . 以下命令将创建名为 daves_branch 的本地分支,跟踪远程分支 origin/daves_branch . 推送更改时,将更新远程分支 .

    对于最新版本的git:

    git checkout --track origin/daves_branch
    

    --trackgit checkout -b [branch] [remotename]/[branch] 的简写,其中[remotename]在这种情况下是 origin 而[branch]是两倍相同,在这种情况下是 daves_branch .

    对于git 1.5.6.5,你需要这个:

    git checkout --track -b daves_branch origin/daves_branch
    

    对于git 1.7.2.3及更高版本,这已经足够了(可能已经提前开始,但这是我能尽快找到的最早确认):

    git checkout daves_branch
    

    请注意,对于最近的git版本,此命令不会创建本地分支,并且会使您处于'detached HEAD'状态 . 如果需要本地分支,请使用 --track 选项 . 详细信息:http://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches

  • 39

    使用 git branch -a (本地和远程分支)或 git branch -r (仅远程分支)查看所有远程及其分支 . 然后,您可以对远程执行 git checkout -t remotes/repo/branch 并创建本地分支 .

    还有一个git ls-remote命令可以查看该远程的所有引用和标记 .

  • 0

    我打字了

    git checkout <branch_name>
    

    得到了

    Branch <branch_name> set up to track remote branch <branch_name> from origin.
    Switched to a new branch '<branch_name>'
    
  • 2

    git fetch && git checkout <your friend's branch name> 应该做的伎俩

  • 2387

    git branch <name> --track origin/<name>

  • 4

    最简单的方法,至少对我来说:

    git fetch origin <branchName>
    
  • 9

    检查远程存在而不是本地存在的myBranch - 这对我有用:

    git fetch --all
    git checkout myBranch
    

    我收到了这条消息:

    Branch myBranch set up to track remote branch myBranch from origin
    Switched to a new branch 'myBranch'
    
  • 4

    如果您正在尝试“签出”新的远程分支(仅存在于远程分支,但不存在于本地),这就是您需要的:

    git fetch origin
    git checkout --track origin/<remote_branch_name>
    

    这假设您要从原点获取 . 如果没有,请用您的远程名称替换origin .

  • 8

    git fetch --all & git checkout <branch name>

  • 16
    git fetch
    
    git branch -r
    
    git checkout <branch_name>
    
  • 91

    有了这个简单的命令:

    git checkout -b 'your_branch' origin/'remote branch'
    
  • 1

    你使用'git pull'来保持你的分支分开 . 我将使用实际的回购和分支名称来帮助,因为'lbranch'和'rbranch'很难破译 .

    我们来使用:

    • myteam.unfuddle.com =远程git服务器

    • tlc =存在回购的虚拟项目帐户

    • daves_branch =远程分支名称

    无论有多少个分支,您或任何同事都可以运行此操作来仅提取您的分支:

    git init
    git pull git@myteam.unfuddle.com:myteam/tlc daves_branch:refs/remotes/origin/daves_branch
    
  • 4

    检查 .git/config ,特别是该遥控器 fetch 上的跟踪 .

    [remote "randomRemote"]
        url = git@github.com:someUser/someRepo.git
        fetch = +refs/heads/*:refs/remotes/randomRemote/*
    

    如果 heads/* 指向 randomRemote/* ,则当您运行 git fetch randomRemote 时,它将获取所有分支 . 然后你可以检查那个分支 .

    除此以外,

    • 您需要使用此功能将远程分支添加到跟踪中 . 运行此项后检查 .git/config . 你会明白的 . git remote set-branches --add randomRemote randomBranch

    • 运行 git fetch randomRemote . 这将获取远程分支 .

    • 现在你可以运行 git checkout randomBranch

  • 3

    The title and the question are confused:

    • Git获取远程分支

    • 我的同事怎么能专门拉那个分支 .

    如果问题是我如何获得远程分支以及如何git checkout远程分支,更简单的解决方案是:

    使用git(> = 1.6.6),您可以使用:

    git checkout <branch_name>
    

    如果找不到本地 <branch_name> 但在一个具有匹配名称的远程中确实存在跟踪分支,则视为等效于:

    git checkout -b <branch_name> --track <remote>/<branch_name>
    

    see documentation for git checkout

    为你的朋友:

    $ git checkout discover
    Branch discover set up to track remote branch discover
    Switched to a new branch 'discover'
    
  • 33

    试试吧

    $ git pull origin your_branch_name

  • 11

    您也可以一次性获取和签出远程分支: -

    git fetch && git checkout the-branch-name
    
  • 12

    我用fetch后跟checkout ...

    git fetch <remote> <rbranch>:<lbranch> 
    git checkout <lbranch>
    

    ...其中 <rbranch> 是远程分支或源ref, <lbranch> 是您想要跟踪的 non-existent 本地分支或目标引用,您可能希望将其命名为远程分支或源ref . 这在_3648670_的解释中在options下解释 .

    Git is so smart it auto completes the first command if I tab after the first few letters of the remote branch. IE: I don't even have to name the local branch, Git automatically copies the name of the remote branch for me. Thanks Git!

    同样如the answer in this similar SO post所示,如果您没有在 fetch 中命名本地分支,则在使用 -b 标志检出时仍可以创建它 . IE: git fetch <remote> <branch> 后跟 git checkout -b <branch> <remote>/<branch> 与我的初始答案完全相同 . 显然,如果您的仓库只有一个遥控器,那么您可以在 fetch 之后执行 git checkout <branch> ,它将为您创建一个本地分支 . EG:你刚刚克隆了一个仓库,想要从遥控器上查看额外的分支 .

    我相信 fetch 的一些文档可能已经从pull逐字复制 . 特别是options<refspec> 的部分是相同的 . 但是,我不相信 fetch 会永远merge,所以如果你离开冒号的目的地一边空 fetch should do nothing .

    注意: git fetch <remote> <refspec>git fetch <remote> <refspec>: 的缩写,因此无效,但 git fetch <remote> <tag>git fetch <remote> <tag>:<tag> 相同,后者应在本地复制远程 <tag> .

    我想这只有在您想在本地复制远程分支时才有用,但不一定要立即检查它 . 否则我现在会使用the accepted answer above,这将在checkout description的第一部分中详细解释,后面在options部分的options部分解释,因为它是1-liner . 嗯......有点像1班轮,因为你仍然必须首先运行 git fetch <remote> .

    仅供参考: <refspecs> (来源:目的地)的顺序解释了deleting remote branches的奇怪的前Git-1.7方法 . IE:不推送任何内容到目的地refspec .

  • 24

    有时会要求您不要使用主分支并仅使用远程分支(正如我被要求的那样) . 所以你需要的只是远程分支 .

    因此,要单独克隆远程分支(没有主服务器),请执行此操作

    git clone url --branch remote_branch_name
    

    其中,remote_branch_name是远程分支的名称

    例如,

    git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git --branch v3.15
    

    这将确保使用远程分支的名称将远程分支克隆到本地分支 .

    现在,如果您提交代码并推送,代码将单独提交给该分支 .

相关问题