Skip to content

git 分支

创建 and managing 分支使用 git 分支 — isolating features and parallel development.

你将学到

  • 如何 创建, 列表, and 删除分支
  • 为什么 branching 是 Git's killer feature
  • difference 之间 git 分支 and git checkout -b
  • Renaming and tracking 分支

概念讲解

Why Branching Matters

Branching lets you diverge from the main line of development and work on something independently — a new feature, a bug fix, an experiment — without affecting the stable code. This is Git's most powerful feature.

git branch feature-login

This creates a new branch, but does not switch to it. To create and switch in one step, use git checkout -b or git 切换 -c.

Listing Branches

git branch              # list local branches (* marks current)
git branch -r           # list remote branches
git branch -a           # list all branches (local + remote)
git branch -v           # show last commit on each branch
git branch -vv          # show tracking info too

The asterisk (*) next to a branch name indicates the branch you are currently on.

Creating and Switching

# Create a branch (stays on current branch)
git branch feature-search

# Switch to it
git checkout feature-search
# or with the newer command:
git switch feature-search

# Create and switch in one step
git checkout -b feature-search
# or:
git switch -c feature-search

Deleting Branches

# Delete a fully-merged branch
git branch -d feature-login

# Force delete even if not merged (use with caution!)
git branch -D feature-login

-d refuses to delete a branch that has unmerged changes, protecting you from losing work. -D forces deletion regardless — only use it when you are sure.

Renaming a Branch

# Rename the current branch
git branch -m new-name

# Rename a specific branch
git branch -m old-name new-name

Branch Naming Conventions

Good branch names are descriptive and follow a pattern:

  • feature/用户-认证
  • bugfix/登录-redirect
  • hotfix/security-patch
  • chore/更新-dependencies
  • docs/API-引用

This makes it clear what each branch is for and enables automation (like auto-generating changelogs).

Lightweight Branches

Git branches are just pointers to a commit — they are incredibly lightweight. Creating a branch takes milliseconds and uses almost no disk space. Unlike SVN, where branching copies an entire directory, Git branches cost nothing. Create branches freely.

示例代码

              # List all branches — * marks the current branch
git branch
# * main
#   feature-login

# Create a new branch for a feature
git branch feature-search

# Switch to the new branch
git checkout feature-search
# or: git switch feature-search

# Create and switch in one step (most common)
git checkout -b feature-payment
# or: git switch -c feature-payment

# Make some changes on the feature branch
echo "payment logic" > payment.js
git add payment.js
git commit -m "feat: add payment module"

# Switch back to main
git checkout main

# List branches with last commit info
git branch -v

# Rename the current branch
git branch -m feature-payments

# Delete a merged branch
git checkout main
git branch -d feature-search

# Force delete an unmerged branch (caution!)
git branch -D experimental-branch

# List remote branches too
git branch -a
            

Covers listing, 创建, 切换, renaming, and 删除分支. checkout -b (or 切换 -c) 模式 for 创建 and 切换 in one 步骤是 大多数通常 used command. 注意 -d 对比 -D distinction for safe 对比 forced deletion.

动手试一试

常见错误

错误写法

创建分支但 forgetting 到切换到 它之前制作更改

正确做法

git 分支 <name> 创建分支但 stays on current one. Use git checkout -b <name> (or git 切换 -c <name>) 到创建 AND 切换 in one 步骤. 总是检查 git 分支到 确认哪个分支你 是 on 之前编辑.

错误写法

Using -D (force 删除) 相反的 -d and losing unmerged 工作

正确做法

git 分支 -d refuses 到删除分支使用 unmerged 更改, protecting 你的工作. 仅 use -D 当你 是绝对地确定分支's 工作是 不需要. 如果你 accidentally 删除分支, 你能 经常 recover 它使用 git reflog.

相关速查表

练习 git 分支

11 个练习

练习 git 分支

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误