git之获取远程分支(fetch5种方法)

Git 分支切换方法

方法一:git checkout targetbranch

  1. 首先,获取远程所有分支
    1
    git fetch
  2. 查看所有远程分支,找到需要的远程分支,例如 origin/targetbranch
    1
    git branch -r
  3. 在本地新建一个同名分支,然后系统会自动与该远程分支关联
    1
    git checkout targetbranch

    注意:git checkout origin/mybranch 会进入 detached head 状态,不会在本地新建分支,不要这样写。

方法二:git checkout -b 本地分支名 origin/远程分支名

  1. 首先,获取远程所有分支
    1
    git fetch
  2. 创建与远程分支关联的本地分支(可以同名,也可以不同名;建议同名,方便管理)
    1
    git checkout -b 本地分支名 origin/远程分支名

方法三:git checkout --track origin/远程分支名

  1. 首先,获取远程所有分支
    1
    git fetch
  2. 创建与远程分支关联的本地分支
    1
    git checkout --track origin/远程分支名

方法四:git checkout -t origin/远程分支名

  1. 首先,获取远程所有分支
    1
    git fetch
  2. 创建与远程分支关联的本地分支
    1
    git checkout -t origin/远程分支名

方法五:fetch 指定的一个分支:git fetch [repo] [remote_branch_name]:[local_branch_name]

  1. 获取远程指定分支,并创建与之关联的本地分支

    1
    git fetch [repo] [remote_branch_name]:[local_branch_name]

    举例:git fetch origin TargetBranch:tempBranch 从远程仓 origin 的 TargetBranch 分支下载到本地,并新建一个 tempBranch 分支。

    [remote_branch_name]:[local_branch_name] 可同名,也可不同名;建议同名,便于管理。

  2. 切换当前分支为本地建立的分支

    1
    git checkout [local_branch_name]