# Git 如何重命名分支

URL: https://caijiao.org/how-to/git/rename-branch
Source: docs/how-to/git/rename-branch.md
Description: Git 如何重命名分支：本地用 git branch -m 改名，已推送分支还需要推送新分支、删除旧远程分支并设置 upstream。

## 快速答案

重命名当前本地分支：

```bash
git branch -m new-branch-name
```

重命名指定本地分支：

```bash
git branch -m old-branch-name new-branch-name
```

如果旧分支已经推送到远程：

```bash
git push origin -u new-branch-name
git push origin --delete old-branch-name
```

## 什么时候需要额外处理远程分支

`git branch -m` 只修改本地分支名。远程仓库不会自动跟着改名，所以已经推送过的分支需要：

1. 推送新分支名。
2. 设置新分支的 upstream。
3. 删除旧远程分支。
4. 通知协作者切换到新分支名。

## 常见坑

- 只改了本地分支名，`git push` 时仍然关联旧 upstream。
- 删除旧远程分支前没有确认 Pull Request 或 CI 配置是否仍引用旧名称。
- 重命名默认分支需要在代码托管平台同步修改默认分支设置。

## 相关内容

- [Git 常见问题](/how-to/git/)
- [git remote 参考](/reference/git/remote)
- [Git 如何删除远程分支](/how-to/git/delete-remote-branch)
- [Git 分支管理](/git/branch/create-switch)
- [Git 官方 branch 文档](https://git-scm.com/docs/git-branch)
