首页·终端命令

git switch

git

用法

git switch [-c <new>] <branch>

git 2.23 引入的命令,只负责在分支之间切换;它从不改写文件内容,那是 restore 的事。

常用选项

选项含义
-c <new>create the branch and switch to it
-C <new>create it, or reset it here if it exists
-go back to the branch you were on before
--detach <commit>check out a commit without a branch
--track <remote>/<branch>follow a remote branch
-mcarry uncommitted changes across by merging them

示例

git switch main

moves to main; refuses if that would clobber your edits

git switch -c feature/api

creates the branch and moves there

git switch -

jumps back to the previous branch

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git switch 是做什么的?

git 2.23 引入的命令,只负责在分支之间切换;它从不改写文件内容,那是 restore 的事。

Q. 怎么写?

git switch [-c <new>] <branch> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-switch