← 返回命令列表

Linux command

git-branch 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

List all local branches

git branch

List all branches

git branch -a

Create a new branch

git branch [branch_name]

Create and switch

git branch [branch_name] && git checkout [branch_name]

Delete a local branch

git branch -d [branch_name]

Force delete a branch

git branch -D [branch_name]

Rename the current branch

git branch -m [new_name]

Set upstream tracking

git branch -u [origin/branch_name]

Show branches with last commit

git branch -v

说明

git branch lists, creates, renames, and deletes branches. Without arguments, it lists existing local branches, marking the current branch with an asterisk. Branches are lightweight pointers to commits, allowing parallel development workflows. Creating a branch does not switch to it; use git checkout or git switch to change branches. Remote-tracking branches (origin/main, etc.) are read-only references to the state of branches on remote repositories. They are updated by git fetch.

参数

-a, --all
List both local and remote-tracking branches.
-r, --remotes
List remote-tracking branches only.
-d, --delete
Delete a branch (must be fully merged).
-D
Force delete a branch regardless of merge status.
-m, --move
Rename a branch.
-M
Force rename even if target name exists.
-c, --copy
Copy a branch.
-u, --set-upstream-to=_UPSTREAM_
Set upstream tracking branch.
--unset-upstream
Remove upstream tracking information.
-v, --verbose
Show SHA1 and commit subject for each branch.
--merged
List branches merged into current branch.
--no-merged
List branches not merged into current branch.
--contains _COMMIT_
List branches containing the specified commit.

FAQ

What is the git-branch command used for?

git branch lists, creates, renames, and deletes branches. Without arguments, it lists existing local branches, marking the current branch with an asterisk. Branches are lightweight pointers to commits, allowing parallel development workflows. Creating a branch does not switch to it; use git checkout or git switch to change branches. Remote-tracking branches (origin/main, etc.) are read-only references to the state of branches on remote repositories. They are updated by git fetch.

How do I run a basic git-branch example?

Run `git branch` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a, --all do in git-branch?

List both local and remote-tracking branches.