Setup and Config
Getting and Creating Projects
Basic Snapshotting
Branching and Merging
Sharing and Updating Projects
Inspection and Comparison
Patching
Debugging
External Systems
Server Admin
Guides
- gitattributes
- Command-line interface conventions
- Everyday Git
- Frequently Asked Questions (FAQ)
- Glossary
- Hooks
- gitignore
- gitmodules
- Revisions
- Submodules
- Tutorial
- Workflows
- All guides...
Administration
Plumbing Commands
- 2.44.0 02/23/24
- 2.43.2 → 2.43.3 no changes
- 2.43.1 02/09/24
- 2.43.0 11/20/23
- 2.38.1 → 2.42.1 no changes
- 2.38.0 10/02/22
- 2.35.1 → 2.37.7 no changes
- 2.35.0 01/24/22
- 2.30.1 → 2.34.8 no changes
- 2.30.0 12/27/20
- 2.27.1 → 2.29.3 no changes
- 2.27.0 06/01/20
概述
git switch [<选项>] [--no-guess] <分支> git switch [<选项>] --detach [<起点>] git switch [<选项>] (-c|-C) <新分支> [<起点>] git switch [<选项>] --orphan <新分支>
描述
切换到指定分支。工作区和索引会更新以匹配该分支。所有新提交的内容都会添加到该分支的顶端。
可选择使用 -c
、-C
,自动从同名的远程分支创建新分支(参见 --guess
),或使用 --detach
,从任何分支分离工作区,同时进行切换。
切换分支不需要干净的索引和工作区(即与 HEAD
相比没有差异)。但是,如果该操作会导致本地变更的丢失,除非使用 --discard-changes
或 --merge
,否则该操作将被中止。
这个命令是试验性的。其行为可能会改变。
选项
- <分支>
-
转到的分支。
- <新分支>
-
新分支的名称。
- <start-point>
-
新分支的起点。指定
<起始点>
后,就可以根据历史上的其他点创建分支,而不是 HEAD 当前指向的点。(或者,在--detach
的情况下,允许你检查并从其他点分离。)You can use the
@{-N}
syntax to refer to the N-th last branch/commit switched to using "git switch" or "git checkout" operation. You may also specify-
which is synonymous to@{-1}
. This is often used to switch quickly between two branches, or to undo a branch switch by mistake.作为一种特殊情况,如果正好有一个合并基础,你可以使用`A…B`作为`A`和`B`的合并基础的快捷方式。你最多可以省略在`A`和`B`中默认为`HEAD`的一个分支。
- -c <新分支>
- --create <新分支>
-
Create a new branch named
<new-branch>
starting at<start-point>
before switching to the branch. This is the transactional equivalent of$ git branch <新分支> $ git switch <新分支>
that is to say, the branch is not reset/created unless "git switch" is successful (e.g., when the branch is in use in another worktree, not just the current branch stays the same, but the branch is not reset to the start-point, either).
- -C <新分支>
- --force-create <新分支>
-
类似于
--create
,但如果<新分支>
已经存在,它将被重置为<起点>
。这是一个方便的快捷方式:$ git branch -f <新分支> $ git switch <新分支>
- -d
- --detach
-
切换到用于检查和可丢弃实验的提交。详见 git-checkout[1] 中的 “分离式 HEAD” 部分。
- --guess
- --no-guess
-
如果没有找到`<分支>
,但确实有一个远程分支(称为
<远程分支>`)的跟踪分支存在,且名称匹配,则视为等同于$ git switch -c <分支> --track <远程仓库>/<分支>
如果该分支存在于多个远程,并且其中一个是由`checkout.defaultRemote`配置变量命名的,为了消除歧义,我们将使用该变量,即使`<分支>
在所有远程中并不唯一。将其设置为例如 `checkout.defaultRemote=origin
,以便在`<分支>不明确但存在于'origin'远程时,总是从那里签出远程分支。参见 git-config[1] 中的 `checkout.defaultRemote
。--guess`是默认行为。使用
--no-guess`来禁用它。默认行为可以通过`checkout.guess`配置变量设置。
- -f
- --force
-
--discard-changes
的别名。 - --discard-changes
-
即使索引或工作树与
HEAD
不同,也要继续。索引和工作树都会被恢复以匹配切换目标。如果指定了--recurse-submodules
,子模块内容也会恢复到与切换目标一致。这用于丢弃本地更改。 - -m
- --merge
-
If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch.
当合并冲突发生时,冲突路径的索引条目会被留下,你需要解决冲突,并用`git add`标记已解决的路径(如果合并应导致删除路径,则用`git rm`)。
- --conflict=<样式>
-
The same as
--merge
option above, but changes the way the conflicting hunks are presented, overriding themerge.conflictStyle
configuration variable. Possible values are "merge" (default), "diff3", and "zdiff3". - -q
- --quiet
-
静默,压制反馈信息。
- --progress
- --no-progress
-
当它附加到终端时,除非指定
--quiet
,否则默认情况下会在标准错误流中报告进度状态。这个标志可以启用进度报告,即使没有附在到终端,而不管--quiet
。 - -t
- --track [direct|inherit]
-
When creating a new branch, set up "upstream" configuration.
-c
is implied. See--track
in git-branch[1] for details.If no
-c
option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the initial part up to the "*". This would tell us to usehack
as the local branch when branching off oforigin/hack
(orremotes/origin/hack
, or evenrefs/remotes/origin/hack
). If the given name has no slash, or the above guessing results in an empty name, the guessing is aborted. You can explicitly give a name with-c
in such a case. - --no-track
-
不设置 "上游 "配置,即使`branch.autoSetupMerge`配置变量为true。
- --orphan <新分支>
-
Create a new unborn branch, named
<new-branch>
. All tracked files are removed. - --ignore-other-worktrees
-
当想要的引用已被另一个工作树签出时,
git switch
会拒绝签出。这个选项会让它无论如何都把引用签出。换句话说,该引用可以被多个工作树持有。 - --recurse-submodules
- --no-recurse-submodules
-
使用
--recurse-submodules
,将根据超级项目中记录的提交更新所有活动子模块的内容。如果什么都不使用(或--no-recurse-submodules
),子模块工作树将不会被更新。就像 git-submodule[1],这会分离子模块的HEAD
。
实例
下面的命令会切换到 "master" 分支:
$ git switch master
在错误的分支工作后,切换到正确的分支将使用:
$ git switch mytopic
但是,您的 "wrong" 分支和正确的 "mytopic" 分支可能在您本地修改的文件中存在差异,在这种情况下,上述开关就会像这样失败:
$ git switch mytopic error: 你对 'footz' 进行了本地更改,而不是切换分支。
你可以给命令加上 `-m`标志,这样就可以尝试三方合并了:
$ git switch -m mytopic 自动合并 frotz 中
在这个三方合并之后,本地的修改并「没有」登记在你的索引文件中,所以`git diff`会显示你在新分支的提示下做了哪些修改。
切换回切换到 mytopic 之前的分支(即 "master" 分支):
$ git switch -
你可以从任何提交创建一个新分支。例如,切换到 "HEAD~3" 并创建分支 "fixup":
$ git switch -c fixup HEAD~3 切换到新分支 'fixup'
如果要从同名的远程分支启动一个新分支:
$ git switch new-topic 设置了分支 'new-topic',以跟踪来自 'origin' 的远程分支 'new-topic' 切换到一个新分支 'new-topic'
查看提交 HEAD~3
以进行临时检查或实验,而无需创建新分支:
$ git switch --detach HEAD~3 HEAD 现为 9fc9555312 合并分支 'cc/shared-index-permbits'
如果发现你所做的任何事情都值得保留,你可以随时为它创建一个新名称(不需要换掉):
$ git switch -c good-surprises
配置
Warning
|
Missing See original version for this content. |
Warning
|
Missing See original version for this content. |
GIT
属于 git[1] 文档