我如何在Git中切换分支?

3
我执行了以下命令以切换到由我的队友创建的新分支:
git checkout with-backend

我收到了下面的错误: 错误: 路径 'with-backend' 没有匹配到 Git 已知的任何文件 我尝试执行以下命令:
git branch -a

我的队友创建的with-backend分支未显示在列表中。以下是列出的结果:
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

2
在执行这些命令之前,请尝试执行 git fetch - Bernardo Duarte
2个回答

3

首先,在Git 2.23(2019年第三季度)之后,切换分支应使用git switch命令而非git checkout命令(该命令试图同时管理文件和分支,因此容易混淆)。

其次,在git fetch之后,git switch with-backend将会工作,因为它有一个“猜测”模式:

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

$ git switch -c <branch> --track <remote>/<branch>

2

在开始工作之前,请确保始终从存储库中拉取最新更改。

git fetch <-- 从远程存储库获取所有最新更改

OR

git pull <-- 比git fetch更进一步,它获取远程更改并将本地分支与远程分支合并


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接