📜  git pull new branch from remote - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:00:56.176000             🧑  作者: Mango

git pull new branch from remote - Shell-Bash

如果您想从远程仓库中获取一个新分支并在本地进行创建和跟踪,则可以使用 git pull 命令。

步骤
  1. 首先,需要将远程仓库添加至本地仓库,将其命名为 origin
git remote add origin <remote_repo_url>
  1. 接下来,需要从远程仓库获取分支列表,以确保要获取的分支实际存在。
git branch -r
  1. 然后,使用 git fetch 命令从远程仓库中拉取分支,并在本地创建和跟踪该分支。
git fetch origin <remote_branch_name>:<local_branch_name>
  1. 如果您只需要获取该分支的最新代码,可以运行 git pull 命令。
git pull origin <remote_branch_name>
示例

以一个名为 myproject 的仓库为例,下面的操作将从该仓库的远程分支中获取一个名为 new-feature 的新分支,并在本地创建和跟踪该分支。

# 将远程仓库添加至本地仓库
git remote add origin https://github.com/myusername/myproject.git

# 获取分支列表
git branch -r

# 从远程仓库拉取分支并在本地创建和跟踪该分支
git fetch origin new-feature:new-feature

# 获取分支的最新代码
git pull origin new-feature

请注意,如果您从远程仓库拉取新分支之前已经运行过 git fetch 命令,则无需再次运行该命令。