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

名称NAME

git-branch - 列出,创建或删除分支

概要SYNOPSIS

git branch [--color[=<when>] | --no-color] [--show-current]
	[-v [--abbrev=<length> | --no-abbrev]]
	[--column[=<options>] | --no-column] [--sort=<key>]
	[(--merged | --no-merged) [<commit>]]
	[--contains [<commit]] [--no-contains [<commit>]]
	[--points-at <object>] [--format=<format>]
	[(-r | --remotes) | (-a | --all)]
	[--list] [<pattern>…​]
git branch [--track | --no-track] [-f] <branchname> [<start-point>]
git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
git branch --unset-upstream [<branchname>]
git branch (-m | -M) [<oldbranch>] <newbranch>
git branch (-c | -C) [<oldbranch>] <newbranch>
git branch (-d | -D) [-r] <branchname>…​
git branch --edit-description [<branchname>]

描述DESCRIPTION

如果给出了--list或没有非选项参数,则列出现有分支; 当前分支将以绿色突出显示并标有星号。 在链接的工作树中签出的所有分支将以青色突出显示并标有加号。 选项-r导致列出远程跟踪分支,选项-a同时显示本地和远程分支。

如果给出了<pattern>,它将用作shell通配符以将输出限制为匹配的分支。 如果给出了多个模式,则如果匹配任何一个模式,则显示出该分支。

请注意,在提供<pattern>时,必须使用--list; 否则,该命令可以解释为分支创建。

使用 --contains,仅显示包含指定提交的分支(换句话说,其尖端提交是指定提交的后代的分支),--no-contains与其相反。

使用--merged时,将仅列出合并到指定提交中的分支(即,可以从指定提交访问其尖端提交的分支)。

使用--no-merged将仅列出未合并到命名提交中的分支。 如果<commit>参数丢失,则默认为HEAD(即当前分支的尖端)。

该命令的第二种形式将创建一个名为<branchname>的新分支头,该分支头指向当前的HEAD或<start-point>(如果指定)。 在特殊情况下,对于<start-point>,如果存在一个合并库,则可以使用“ A ... B”作为A和B合并库的快捷方式。 您最多可以省略A和B中的一个,在这种情况下,它默认为HEAD。

请注意,这将创建新分支,但不会将工作树切换到该分支。 使用“ git switch <newbranch>”切换到新分支。

当本地分支从远程跟踪分支开始时,Git会设置分支(特别是branch.<name>.remote和branch.<name>.merge配置条目),以便git pull将适当地从远程跟踪分支合并。 可以通过globalbranch.autoSetupMerge配置标志来更改此行为。 可以使用--track和--no-track选项覆盖该设置,稍后使用git branch --set-upstream-to进行更改。

使用 -m 或 -M 选项,<oldbranch>将重命名为<newbranch>。 如果<oldbranch>具有相应的引用日志,则将其重命名以匹配<newbranch>,并创建引用日志条目以记住分支重命名。 如果<newbranch>存在,则必须使用-M强制进行重命名。

c和-C选项具有与-m和-M完全相同的语义,除了不是将分支连同其config和reflog重命名之外,它将被复制为新名称。

使用-d或-D选项将删除<branchname>。 您可以指定多个分支进行删除。 如果分支当前有一个参考日志,则该参考日志也将被删除。

Use -r together with -d to delete remote-tracking branches.

一起使用-d -r 删除远程跟踪分支。

请注意,只有在远程存储库中不再存在远程跟踪分支或将git fetch配置为不再再次获取它们时,才有意义删除远程跟踪分支。 另请参阅git-remote [1]的prune子命令,以清除所有过时的远程跟踪分支。

选项OPTIONS

-d    --delete

删除分支。 如果未使用--track或--set-upstream-to设置上游,则该分支必须在其上游分支或HEAD中完全合并。

-D

Shortcut for --delete --force.

--create-reflog

创建分支的引用日志。

This activates recording of all changes made to the branch ref, enabling use of date based sha1 expressions such as "<branchname>@{yesterday}". Note that in non-bare repositories, reflogs are usually enabled by default by the core.logAllRefUpdates config option. The negated form --no-create-reflog only overrides an earlier --create-reflog, but currently does not negate the setting of core.logAllRefUpdates.

-f    --force

将<branchname>重置为<startpoint>,即使<branchname>已经存在。

如果不使用-f,则git branch拒绝更改现有分支。

与-d(或--delete)结合使用,无论其合并状态如何都允许删除该分支。

与-m(或--move)结合使用,即使新的分支名称已经存在也允许重命名分支,-c(或--copy)也是如此。 

-m    --move

移动/重命名分支和相应的引用日志。

-M

Shortcut for --move --force.

-c    --copy

复制分支和相应的引用日志。

-C

Shortcut for --copy --force.

--color[=<when>]

用颜色高亮标记突出显示当前分支,本地和远程跟踪分支。 该值必须是 always(默认),never或auto。

--no-color

即使配置文件将用颜色高亮标记输出设置为默认值,也要关闭分支颜色。 与--color=never相同。

-i    --ignore-case

排序和过滤分支不区分大小写。

--column[=<options>]

--no-column

Display branch listing in columns. See configuration variable column.branch for option syntax.--column and --no-column without options are equivalent to always and never respectively.

This option is only applicable in non-verbose mode.

-r    --remotes

列出或删除(如果与-d一起使用)远程跟踪分支。 与--list结合使用以匹配可选模式。

-a    --all

列出远程跟踪分支和本地分支。 与--list结合使用以匹配可选模式。

-l    --list

列出分支。 使用可选的<pattern>...,例如 git branch --list 'maint- *',仅列出与模式匹配的分支。

--show-current

打印当前分支的名称。 在分离的HEAD状态下,不打印任何内容。

-v

-vv

--verbose

在列表模式下,显示sha1并提交每个标题的主题行,以及与上游分支的关系(如果有)。 如果给出两次,则同时打印链接的工作树的路径(如果有的话)和上游分支的名称(另请参见git remote show <remote>)。

请注意,当前工作树的HEAD不会打印其路径(它将始终是您的当前目录)。

-q    --quiet

Be more quiet when creating or deleting a branch, suppressing non-error messages.

--abbrev=<length>

Alter the sha1’s minimum display length in the output listing. The default value is 7 and can be overridden by the core.abbrev config option.

--no-abbrev

Display the full sha1s in the output listing rather than abbreviating them.

-t    --track

创建新分支时,请设置branch.<name>.remotebranch.<name>.merge配置条目以将起点(start-point)分支标记为新分支的“上游upstream”。 此配置将告诉git在git status 和git branch -v中显示两个分支之间的关系。 此外,当签出新分支时,它会指示不带参数的git pull从上游拉出。

当起点是远程跟踪分支时,此行为是默认行为。如果您想要git switch,git checkout和git branch始终表现得像使用选项--no-track一样,请将branch.autoSetupMerge配置变量设置为false。 如果您希望当起始点是本地或远程跟踪分支时,是这样的行为,请将其设置为"always''。

--no-track

即使branch.autoSetupMerge配置变量为true,也不要设置“上游”配置。

--set-upstream

由于此选项具有令人困惑的语法,因此不再受支持。 请改为使用--track或--setup-upstream-to。

-u <upstream>

--set-upstream-to=<upstream>

设置<branchname>的跟踪信息,以便将<upstream>视为<branchname>的上游分支。 如果未指定<branchname>,则默认为当前分支。

--unset-upstream

删除<branchname>的上游信息。 如果未指定任何分支,则默认为当前分支。

--edit-description

打开编辑器并编辑文本以解释分支的用途,将由其他各种命令使用(例如format-patchrequest-pullmerge (如果启用))。 可以使用多行说明。

--contains [<commit>]

仅列出包含指定提交的分支(如果未指定,则为HEAD)。Implies --list.

--no-contains [<commit>]

Only list branches which don’t contain the specified commit (HEAD if not specified). Implies --list.

--merged [<commit>]

仅列出其尖端可从指定提交到达的分支(如果未指定,则为HEAD)。Implies --list, incompatible with --no-merged.

--no-merged [<commit>]

Only list branches whose tips are not reachable from the specified commit (HEAD if not specified). Implies --list, incompatible with --merged.

<branchname>

要创建或删除的分支的名称。 新的分支名称必须通过git-check-ref-format [1]定义的所有检查。 其中一些检查可能会限制分支名称中允许的字符。

<start-point>

新的分支头将指向此提交。 它可以以 分支名称,提交ID或tag给出。 如果省略此选项,则将使用当前的HEAD。

<oldbranch>

The name of an existing branch to rename.

<newbranch>

The new name for an existing branch. The same restrictions as for <branchname> apply.

--sort=<key>

根据给定的键排序。

Prefix - to sort in descending order of the value. You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key. The keys supported are the same as those in git for-each-ref. Sort order defaults to the value configured for thebranch.sort variable if exists, or to sorting based on the full refname (including refs/...prefix). This lists detached HEAD (if present) first, then local branches and finally remote-tracking branches. See git-config[1].

--points-at <object>

仅列出给定对象的分支。

--format <format>

A string that interpolates %(fieldname) from a branch ref being shown and the object it points at. The format is the same as that of git-for-each-ref[1].

注意NOTES

如果要创建要立即切换到的分支,则使用“ git switch”命令的带有-c选项可以更容易地通过单个命令执行相同的操作。

选项 --contains--no-contains--merged--no-merged具有四个相关但不同的目的:

  • --contains <commit> 用于查找所有需要特别注意的分支,如果<commit>是rebase或amended的,因为这些分支包含指定的<commit>。

  • --no-contains <commit> is the inverse of that, i.e. branches that don’t contain the specified <commit>.

  • --merged 用于查找可以安全删除的所有分支,因为这些分支完全由HEAD包含。.

  • --no-merged is used to find branches which are candidates for merging into HEAD, since those branches are not fully contained by HEAD.

发布了243 篇原创文章 · 获赞 138 · 访问量 138万+

猜你喜欢

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