Skip to content

git switch

注意

git switch 只在 Git 2.23 及以上版本中可用。如果使用旧版本的 Git,可以继续使用 git checkout 命令进行分支切换和检出。

简介

git switch 是 Git 2.23 版本中引入的一条新命令,它主要用于切换分支、检出某个分支或创建新的分支。相对于旧的命令 git checkoutgit switch 更加清晰、简洁。

shell
git switch [options] <branch> [commit]
  • -c: 创建并切换到新分支;
  • -C:创建并切换到新分支,若存在则重置;
  • -d: 创建临时分支。

常见用法

  • 切换分支git switch branch_name,切换到指定名称的分支。
  • 检出某个提交记录git switch commit_id,检出指定的提交记录,创建一个临时分支。
  • 创建新分支git switch -c new_branch_name,创建一个新的分支,并切换到该分支。
  • 查看git switch -d branch_name,查看指定提交。
  • 切换到上一个分支git switch -,切换到上一个分支。
  • 切换到远程分支git switch -c new_branch_name origin/remote_branch_name,创建一个新的本地分支并从远程分支检出。
  • 要求清空工作目录git switch --discard-changes branch_name,强制切换到另一个分支时丢弃所有未提交的更改,包括未暂存和暂存但未提交的更改。