# git-branch
> 原文: [https://git-scm.com/docs/git-branch](https://git-scm.com/docs/git-branch)
## 名稱
git-branch - 列出,創建或刪除分支
## 概要
```
git branch [--color[=<when>] | --no-color] [-r | -a]
[--list] [-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>] [<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>]
```
## 描述
如果給出`--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 checkout< newbranch>”切換到新分支。
當本地分支從遠程跟蹤分支啟動時,Git設置分支(特別是`branch.<name>.remote`和`branch.<name>.merge`配置條目),以便 _git pull_ 能適當地從遠程跟蹤分支合并本地。可以通過全局`branch.autoSetupMerge`配置標志更改此行為。可以使用`--track`和`--no-track`選項覆蓋該設置,稍后使用`git branch --set-upstream-to`進行更改。
使用`-m`或`-M`選項,<oldbranch>將重命名為<newbranch>。如果<oldbranch>有一個相應的reflog,它被重命名為匹配<newbranch>,并創建一個reflog條目來記住分支重命名。如果<newbranch>存在,-M必須用于強制重命名發生。
`-c`和`-C`選項具有與`-m`和`-M`完全相同的語義,除了將分支與其配置重命名,并且reflog將被復制到新名稱。
使用`-d`或`-D`選項,`<branchname>`將被刪除。您可以指定多個分支進行刪除。如果分支當前有reflog,那么reflog也將被刪除。
將`-r`與`-d`一起使用以刪除遠程跟蹤分支。請注意,如果遠程存儲庫中不再存在遠程跟蹤分支,或者如果 _git fetch_ 配置為不再獲取它們,則刪除它們才有意義。另請參閱 [git-remote [1]](https://git-scm.com/docs/git-remote) 的 _prune_ 子命令,了解清除所有過時的遠程跟蹤分支的方法。
## 選項
```
-d
```
```
--delete
```
刪除分支。分支必須在其上游分支中完全合并,或者如果沒有使用`--track`或`--set-upstream-to`設置上游,則必須在`HEAD`中合并。
```
-D
```
`--delete --force`的快捷方式。
```
--create-reflog
```
創建分支的reflog。這將激活記錄對分支引用所做的所有更改,從而可以使用基于日期的sha1表達式,例如“<branchname> @ {yesterday}”。請注意,在非裸存儲庫中,默認情況下,`core.logAllRefUpdates`配置選項通常會啟用reflog。否定形式`--no-create-reflog`僅覆蓋較早的`--create-reflog`,但目前不會否定`core.logAllRefUpdates`的設置。
```
-f
```
```
--force
```
重置<branchname>到<startpoint>,即使<branchname>已存在。沒有`-f`, _git branch_ 拒絕更改現有分支。與`-d`(或`--delete`)結合使用,允許刪除分支,而不管其合并狀態如何。與`-m`(或`--move`)結合使用,即使新分支名稱已存在,也允許重命名分支,這同樣適用于`-c`(或`--copy`)。
```
-m
```
```
--move
```
移動/重命名分支和相應的reflog。
```
-M
```
`--move --force`的快捷方式。
```
-c
```
```
--copy
```
復制分支和相應的reflog。
```
-C
```
`--copy --force`的快捷方式。
```
--color[=<when>]
```
顏色分支以突出顯示當前,本地和遠程跟蹤分支。該值必須為always(默認值),never或auto。
```
--no-color
```
即使配置文件提供默認的顏色輸出,也要關閉分支顏色。與`--color=never`相同。
```
-i
```
```
--ignore-case
```
排序和過濾分支不區分大小寫。
```
--column[=<options>]
```
```
--no-column
```
在列中顯示分支列表。有關選項語法,請參閱配置變量column.branch。沒有選項的`--column`和`--no-column`分別相當于 _always_ 和 _never_。
此選項僅適用于非詳細模式。
```
-r
```
```
--remotes
```
列出或刪除(如果與-d一起使用)遠程跟蹤分支。
```
-a
```
```
--all
```
列出遠程跟蹤分支和本地分支。
```
-l
```
```
--list
```
列出分支。使用可選的`<pattern>...`,例如`git branch --list 'maint-*'`,僅列出與模式匹配的分支。
```
-v
```
```
-vv
```
```
--verbose
```
在列表模式下,顯示每個頭的sha1和提交主題行,以及與上游分支(如果有)的關系。如果給出兩次,也打印上游分支的名稱(另請參見`git remote show <remote>`)。
```
-q
```
```
--quiet
```
在創建或刪除分支時更安靜,禁止出現非錯誤消息。
```
--abbrev=<length>
```
在輸出列表中更改sha1的最小顯示長度。默認值為7,可以通過`core.abbrev`配置選項覆蓋。
```
--no-abbrev
```
在輸出列表中顯示完整的sha1,而不是縮寫它們。
```
-t
```
```
--track
```
創建新分支時,設置`branch.<name>.remote`和`branch.<name>.merge`配置條目以將起點分支標記為新分支的“上游”。此配置將告訴git顯示`git status`和`git branch -v`中兩個分支之間的關系。此外,它在沒有參數的情況下指示`git pull`在檢出新分支時從上游拉出。
當起點是遠程跟蹤分支時,此行為是默認行為。如果希望`git checkout`和`git branch`始終表現得像`--no-track`一樣,請將branch.autoSetupMerge配置變量設置為`false`。如果在起點是本地或遠程跟蹤分支時需要此行為,請將其設置為`always`。
```
--no-track
```
即使branch.autoSetupMerge配置變量為true,也不要設置“上游”配置。
```
--set-upstream
```
由于此選項具有令人困惑的語法,因此不再支持它。請改用`--track`或`--set-upstream-to`。
```
-u <upstream>
```
```
--set-upstream-to=<upstream>
```
設置<branchname>的跟蹤信息,以便<upstream>被認為是<branchname>的上游分支。如果<branchname>沒有指定上游分支,則默認為當前分支。
```
--unset-upstream
```
刪除<branchname>的上游信息。如果未指定分支,則默認為當前分支。
```
--edit-description
```
打開編輯器并編輯文本以解釋分支的用途,供各種其他命令使用(例如`format-patch`,`request-pull`和`merge`(如果已啟用))。可以使用多行解釋。
```
--contains [<commit>]
```
僅列出包含指定提交的分支(如果未指定,則為HEAD)。意味著`--list`。
```
--no-contains [<commit>]
```
僅列出不包含指定提交的分支(如果未指定,則為HEAD)。意味著`--list`。
```
--merged [<commit>]
```
僅列出其提示可從指定提交到達的分支(如果未指定,則為HEAD)。意味著`--list`,與`--no-merged`不兼容。
```
--no-merged [<commit>]
```
僅列出其提示無法從指定的提交訪問的分支(如果未指定,則為HEAD)。意味著`--list`,與`--merged`不兼容。
```
<branchname>
```
要創建或刪除的分支的名稱。新分支名稱必須通過 [git-check-ref-format [1]](https://git-scm.com/docs/git-check-ref-format) 定義的所有檢查。其中一些檢查可能會限制分支名稱中允許的字符。
```
<start-point>
```
新的分支頭將指向此提交。它可以作為分支名稱,commit-id或標記給出。如果省略此選項,則將使用當前HEAD。
```
<oldbranch>
```
要重命名的現有分支的名稱。
```
<newbranch>
```
現有分支的新名稱。與<branchname>相同的限制應用。
```
--sort=<key>
```
根據給定的密鑰排序。前綴`-`按值的降序排序。您可以使用--sort =<key>選項多次,在這種情況下,最后一個鍵成為主鍵。支持的鍵與`git for-each-ref`中的鍵相同。排序順序默認為`branch.sort`變量(如果存在)配置的值,或基于完整refname(包括`refs/...`前綴)的排序。這首先列出分離的HEAD(如果存在),然后是本地分支,最后是遠程跟蹤分支。見 [git-config [1]](https://git-scm.com/docs/git-config) 。
```
--points-at <object>
```
僅列出給定對象的分支。
```
--format <format>
```
在分支ref和它指向的對象中插入顯示`%(fieldname)`的字符串。格式與 [git-for-each-ref [1]](https://git-scm.com/docs/git-for-each-ref) 的格式相同。
## 配置
僅在列出分支時,當使用或引用`--list`時,才會遵守`pager.branch`。默認是使用pager。見 [git-config [1]](https://git-scm.com/docs/git-config) 。
## 例子
```
Start development from a known tag
```
```
$ git clone git://git.kernel.org/pub/scm/.../linux-2.6 my2.6
$ cd my2.6
$ git branch my2.6.14 v2.6.14 (1)
$ git checkout my2.6.14
```
1. 這一步和下一步可以通過“checkout -b my2.6.14 v2.6.14”組合成一個步驟。
```
Delete an unneeded branch
```
```
$ git clone git://git.kernel.org/.../git.git my.git
$ cd my.git
$ git branch -d -r origin/todo origin/html origin/man (1)
$ git branch -D test (2)
```
1. 刪除遠程跟蹤分支“todo”,“html”和“man”。下次 _fetch_ 或 _pull_ 將再次創建它們,除非你不配置它們。參見 [git-fetch [1]](https://git-scm.com/docs/git-fetch) 。
2. 即使“master”分支(或當前檢出的任何分支)沒有來自test分支的所有提交,也要刪除“test”分支。
## 注意
如果要創建要立即簽出的分支,則可以更輕松地使用git checkout命令及其`-b`選項來創建分支并使用單個命令將其簽出。
選項`--contains`,`--no-contains`,`--merged`和`--no-merged`有四個相關但不同的用途:
* `--contains <commit>`用于在所有分支中查找包含指定<commit>的分支。如果<commit>是被重新定義或修改的,需要特別注意。
* `--no-contains <commit>`與此相反,即不包含指定的<commit>的分支。
* `--merged`用于查找可以安全刪除的所有分支,因為這些分支完全被HEAD包含。
* `--no-merged`用于查找要合入HEAD的候選分支,因為這些分支未被HEAD完全包含。
## 也可以看看
[git-check-ref-format [1]](https://git-scm.com/docs/git-check-ref-format) , [git-fetch [1]](https://git-scm.com/docs/git-fetch) , [git-remote [1]](https://git-scm.com/docs/git-remote) ,[“了解歷史:什么是分支?“](user-manual.html#what-is-a-branch)在Git用戶手冊中。
## GIT
部分 [git [1]](https://git-scm.com/docs/git) 套件
- git
- git-config
- git-help
- git-init
- git-clone
- git-add
- git-status
- git-diff
- git-commit
- git-reset
- git-rm
- git-mv
- git-branch
- git-checkout
- git-merge
- git-mergetool
- git-log
- git-stash
- git-tag
- git-worktree
- git-fetch
- git-pull
- git-push
- git-remote
- git-submodule
- git-show
- git-log
- git-shortlog
- git-describe
- git-apply
- git-cherry-pick
- git-rebase
- git-revert
- git-bisect
- git-blame
- git-grep
- gitattributes
- giteveryday
- gitglossary
- githooks
- gitignore
- gitmodules
- gitrevisions
- gittutorial
- gitworkflows
- git-am
- git-format-patch
- git-send-email
- git-request-pull
- git-svn
- git-fast-import
- git-clean
- git-gc
- git-fsck
- git-reflog
- git-filter-branch
- git-instaweb
- git-archive
- git-bundle
- git-daemon
- git-update-server-info
- git-cat-file
- git-check-ignore
- git-checkout-index
- git-commit-tree
- git-count-objects
- git-diff-index
- git-for-each-ref
- git-hash-object
- git-ls-files
- git-merge-base
- git-read-tree
- git-rev-list
- git-rev-parse
- git-show-ref
- git-symbolic-ref
- git-update-index
- git-update-ref
- git-verify-pack
- git-write-tree