git - 切换到哪个分支,本地就下载了哪个分支的代码嘛?

查看:178
本文介绍了git - 切换到哪个分支,本地就下载了哪个分支的代码嘛?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

切换到哪个分支,本地就下载了哪个分支的代码嘛?

解决方案

如果是大家一起协作的情况,可能其他人推了其他分支到中央仓库,这时候你本地是看不到的。
比如原来只有master分支,另外一个人推送了f1分支。
这时候在你的本地,

λ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

直接执行切换分支到f1

λ git checkout f1
error: pathspec 'f1' did not match any file(s) known to git.

当然是不行的啦。
那么应该怎么做呢?

你可以把中央的分支信息取下来

λ git fetch origin
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://git.coding.net/xxx/xxx
 * [new branch]      f1         -> origin/f1

λ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/f1
  remotes/origin/master

这样你就能愉快地切换分支了

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

此时你已经在f1分支了,

λ git branch -a
* f1
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/f1
  remotes/origin/master

*在前面表示本地当前所指向的分支。

这篇关于git - 切换到哪个分支,本地就下载了哪个分支的代码嘛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆