首页·终端命令

git branch

git

用法

git branch [-a|-vv] [-d <name>]

在本地列出、创建和删除分支;删掉本地的不会动远端,服务器那边还要 git push origin --delete。

常用选项

选项含义
-alocal and remote-tracking branches
-rremote-tracking branches only
-vvlast commit and upstream of each branch
-d <name>delete a branch that is already merged
-D <name>delete it even if unmerged; those commits are left behind
-m <old> <new>rename
--mergedbranches already contained in the current one
--show-currentprint the branch you are on

示例

git branch -vv

every local branch with its upstream and last commit

git branch -d old-feature

removes a merged branch locally only

git push origin --delete old-feature

the separate step that removes it on the server

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git branch 是做什么的?

在本地列出、创建和删除分支;删掉本地的不会动远端,服务器那边还要 git push origin --delete。

Q. 怎么写?

git branch [-a|-vv] [-d <name>] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 8 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-branch