Git - 将远程仓库里的指定分支代码拉取到本地(本地不存在的分支)
本文转自:https://blog.csdn.net/qq_42764468/article/details/126406190
背景:在远程仓库创建了一个分支feature-haha,本地不存在这个分支
当我想从远程仓库里拉取一条本地不存在的分支时,下面命令将会自动创建一个新的本地分支,并与指定的远程分支关联起来:
git checkout -b 本地分支名 origin/远程分支名
① 远程仓库里有个分支 feature-haha,我本地没有该分支,我要把 feature-haha 代码拉到我本地,执行:
git checkout -b feature-haha origin/feature-haha
若成功,将会在本地创建新分支 feature-haha,并自动切到 feature-haha 上。
若失败,将会出现以下提示:
User@6-cpgh0012 MINGW64 /d/mycode/company_frame/company_member (feature-hh) $ git checkout -b feature-haha origin/feature-haha fatal: 'origin/feature-haha' is not a commit and a branch 'feature-haha' cannot be created from it
② 此时表示拉取不成功,需要先执行:git fetch
User@6-cpgh0012 MINGW64 /d/mycode/company_frame/company_member (feature-hh) $ git fetch From https://gitee.com/chahuaxiaozhan/company_member * [new branch] feature-haha -> origin/feature-haha * [new branch] feature-hh -> origin/feature-hh
③ 再执行:git checkout -b feature-haha origin/feature-haha
User@6-cpgh0012 MINGW64 /d/mycode/company_frame/company_member (feature-hh) $ git checkout -b feature-haha origin/feature-haha Switched to a new branch 'feature-haha' Branch 'feature-haha' set up to track remote branch 'feature-haha' from 'origin'. User@6-cpgh0012 MINGW64 /d/mycode/company_frame/company_member (feature-haha) $ git pull Already up to date.
真实场景:
远程有一个分支:dev-offline-upgrade-v1,现在想基于 dev-offline-upgrade-v1 开发
$ git fetch $ git checkout -b dev-offline-upgrade-v1 origin/dev-offline-upgrade-v1 $ git checkout feature-upgrade-ghh $ git push origin feature-upgrade-ghh
浙公网安备 33010602011771号