GIT分支和合并命令之git-switch

名称NAME

git-switch - 切换分支

概要SYNOPSIS

git switch [<options>] [--no-guess] <branch>
git switch [<options>] --detach [<start-point>]
git switch [<options>] (-c|-C) <new-branch> [<start-point>]
git switch [<options>] --orphan <new-branch>

描述DESCRIPTION

切换到指定的分支。 工作树和索引将更新以匹配分支。 所有新的提交都将添加到该分支的尖端中。

可选地,可以使用-c,-C从同名的远程分支中自动创建一个新分支(请参阅--guess),或者使用--detach在切换的时候,将工作树从任何分支中分离出来。

切换分支不需要干净的索引和工作树(即与HEAD相比没有区别)。 但是,如果该操作导致丢失本地更改,则该操作将中止,除非另行告知--discard-changes或 --merge

选项OPTIONS

<branch>

Branch to switch to.

<new-branch>

Name for the new branch.

<start-point>

新分支的起点。

指定一个<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.

As a special case, you may use A...B as a shortcut for the merge base of A and B if there is exactly one merge base. You can leave out at most one of A and B, in which case it defaults to HEAD.

-c <new-branch>

--create <new-branch>

从<start-point>开始创建一个名为<new-branch>的新分支,并切换到该分支。

这是下面这两个命令组合的一个方便的快捷方式:

$ git branch <new-branch>
$ git switch <new-branch>

-C <new-branch>

--force-create <new-branch>

类似于--create,但如果<new-branch>已经存在,它将被重置为<start-point>。

This is a convenient shortcut for:

$ git branch -f <new-branch>
$ git switch <new-branch>

-d    --detach

切换到一个提交进行检查和可丢弃实验。

See the "DETACHED HEAD" section ingit-checkout[1] for details.

--guess

--no-guess

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>

If the branch exists in multiple remotes and one of them is named by the checkout.defaultRemote configuration variable, we’ll use that one for the purposes of disambiguation, even if the <branch> isn’t unique across all remotes. Set it to e.g. checkout.defaultRemote=origin to always checkout remote branches from there if <branch> is ambiguous but exists on the origin remote. See also checkout.defaultRemote in git-config[1].

--guess is the default behavior. Use --no-guess to disable it.

-f    --force

An alias for --discard-changes.

--discard-changes

Proceed even if the index or the working tree differs from HEAD. Both the index and working tree are restored to match the switching target. If --recurse-submodules is specified, submodule content is also restored to match the switching target. This is used to throw away local changes.

-m    --merge

如果你对一个或多个文件的本地修改在当前分支和您要切换到的分支之间不同,则该命令将拒绝切换分支以保留上下文中的修改。 但是,使用此选项,将在当前分支,你的工作树内容和新分支之间进行三向合并,并且您将位于新分支上。

发生合并冲突时,冲突路径的索引条目将保持未合并状态,你需要解决冲突并使用git add标记已解决的路径(或者,如果合并会导致路径的删除,可以使用git rm)。

--conflict=<style>

The same as --merge option above, but changes the way the conflicting hunks are presented, overriding the merge.conflictStyle configuration variable. Possible values are "merge" (default) and "diff3" (in addition to what is shown by "merge" style, shows the original contents).

-q

--quiet

Quiet, suppress feedback messages.

--progress

--no-progress

Progress status is reported on the standard error stream by default when it is attached to a terminal, unless --quiet is specified. This flag enables progress reporting even if not attached to a terminal, regardless of --quiet.

-t

--track

When creating a new branch, set up "upstream" configuration. -c is implied. See --track in git-branch[1] for details.

创建新分支时,请设置“上游”配置。 -c选项隐含这个选项。 有关详细信息,请参见git-branch [1]中的--track。

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 use hack as the local branch when branching off of origin/hack (or remotes/origin/hack, or even refs/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

Do not set up "upstream" configuration, even if the branch.autoSetupMerge configuration variable is true.

--orphan <new-branch>

Create a new orphan branch, named <new-branch>. All tracked files are removed.

--ignore-other-worktrees

git switch refuses when the wanted ref is already checked out by another worktree. This option makes it check the ref out anyway. In other words, the ref can be held by more than one worktree.

--recurse-submodules

--no-recurse-submodules

Using --recurse-submodules will update the content of all initialized submodules according to the commit recorded in the superproject. If nothing (or --no-recurse-submodules) is used, the work trees of submodules will not be updated.

Just like git-submodule[1], this will detach HEAD of the submodules.

例子EXAMPLES

The following command switches to the "master" branch:

$ git switch master

After working in the wrong branch, switching to the correct branch would be done using:

$ git switch mytopic

However, your "wrong" branch and correct "mytopic" branch may differ in files that you have modified locally, in which case the above switch would fail like this:

$ git switch mytopic
error: You have local changes to 'frotz'; not switching branches.

You can give the -m flag to the command, which would try a three-way merge:

$ git switch -m mytopic
Auto-merging frotz

After this three-way merge, the local modifications are not registered in your index file, so git diffwould show you what changes you made since the tip of the new branch.

To switch back to the previous branch before we switched to mytopic (i.e. "master" branch):

$ git switch -

You can grow a new branch from any commit. For example, switch to "HEAD~3" and create branch "fixup":

$ git switch -c fixup HEAD~3
Switched to a new branch 'fixup'

If you want to start a new branch from a remote branch of the same name:

$ git switch new-topic
Branch 'new-topic' set up to track remote branch 'new-topic' from 'origin'
Switched to a new branch 'new-topic'

To check out commit HEAD~3 for temporary inspection or experiment without creating a new branch:

$ git switch --detach HEAD~3
HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'

If it turns out whatever you have done is worth keeping, you can always create a new name for it (without switching away):

$ git switch -c good-surprises
发布了243 篇原创文章 · 获赞 138 · 访问量 138万+

猜你喜欢

转载自blog.csdn.net/ystyaoshengting/article/details/104075128