mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
226 lines
3.6 KiB
Plaintext
226 lines
3.6 KiB
Plaintext
---
|
|
title: git branch
|
|
description: List, create, or delete branches
|
|
---
|
|
|
|
Manage branches in your repository.
|
|
|
|
## Syntax
|
|
|
|
```bash
|
|
git branch [options] [<branch-name>]
|
|
```
|
|
|
|
## Description
|
|
|
|
`git branch` lets you create, list, rename, and delete branches. Branches allow you to develop features, fix bugs, and experiment safely without affecting the main codebase.
|
|
|
|
## Common usage
|
|
|
|
List all local branches:
|
|
|
|
```bash
|
|
git branch
|
|
```
|
|
|
|
Create a new branch:
|
|
|
|
```bash
|
|
git branch feature-name
|
|
```
|
|
|
|
Delete a branch:
|
|
|
|
```bash
|
|
git branch -d branch-name
|
|
```
|
|
|
|
## Options
|
|
|
|
<ParamField path="-a, --all" type="flag">
|
|
List both local and remote branches
|
|
|
|
```bash
|
|
git branch -a
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-d, --delete" type="flag">
|
|
Delete a fully merged branch
|
|
|
|
```bash
|
|
git branch -d feature-branch
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-D" type="flag">
|
|
Force delete a branch regardless of merge status
|
|
|
|
```bash
|
|
git branch -D unmerged-branch
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-m, --move" type="flag">
|
|
Rename a branch
|
|
|
|
```bash
|
|
git branch -m old-name new-name
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-r, --remotes" type="flag">
|
|
List remote-tracking branches
|
|
</ParamField>
|
|
|
|
<ParamField path="-v, --verbose" type="flag">
|
|
Show branch details including last commit
|
|
</ParamField>
|
|
|
|
## Examples
|
|
|
|
### List branches
|
|
|
|
```bash
|
|
# Local branches only
|
|
git branch
|
|
|
|
# All branches
|
|
git branch -a
|
|
|
|
# Remote branches only
|
|
git branch -r
|
|
|
|
# With commit details
|
|
git branch -v
|
|
```
|
|
|
|
### Create a branch
|
|
|
|
```bash
|
|
git branch new-feature
|
|
```
|
|
|
|
This creates the branch but doesn't switch to it. To switch:
|
|
|
|
```bash
|
|
git checkout new-feature
|
|
```
|
|
|
|
Or create and switch in one command:
|
|
|
|
```bash
|
|
git checkout -b new-feature
|
|
```
|
|
|
|
### Rename a branch
|
|
|
|
```bash
|
|
# Rename current branch
|
|
git branch -m new-name
|
|
|
|
# Rename different branch
|
|
git branch -m old-name new-name
|
|
```
|
|
|
|
### Delete a branch
|
|
|
|
```bash
|
|
# Delete if fully merged
|
|
git branch -d feature-branch
|
|
|
|
# Force delete even if not merged
|
|
git branch -D experimental-branch
|
|
```
|
|
|
|
### Delete a remote branch
|
|
|
|
```bash
|
|
git push origin --delete branch-name
|
|
```
|
|
|
|
## Branch naming conventions
|
|
|
|
Use clear, descriptive names:
|
|
|
|
**Good names:**
|
|
- `feature/user-authentication`
|
|
- `bugfix/login-error`
|
|
- `docs/api-guide`
|
|
- `refactor/database-layer`
|
|
|
|
**Poor names:**
|
|
- `temp`
|
|
- `test`
|
|
- `my-branch`
|
|
- `fix`
|
|
|
|
## Common workflows
|
|
|
|
### Feature branch workflow
|
|
|
|
```bash
|
|
# Create and switch to new branch
|
|
git checkout -b feature/new-feature
|
|
|
|
# Work on feature
|
|
git add .
|
|
git commit -m "Add new feature"
|
|
|
|
# Push to remote
|
|
git push -u origin feature/new-feature
|
|
|
|
# After merging, delete local branch
|
|
git checkout main
|
|
git branch -d feature/new-feature
|
|
```
|
|
|
|
### List branches with details
|
|
|
|
```bash
|
|
git branch -vv
|
|
```
|
|
|
|
Shows local branches with their tracking remote branches and last commit.
|
|
|
|
### Clean up merged branches
|
|
|
|
```bash
|
|
# List merged branches
|
|
git branch --merged
|
|
|
|
# Delete merged branches
|
|
git branch -d old-feature another-old-feature
|
|
```
|
|
|
|
## Tracking branches
|
|
|
|
### Set upstream tracking
|
|
|
|
```bash
|
|
git branch --set-upstream-to=origin/main
|
|
```
|
|
|
|
### View tracking relationships
|
|
|
|
```bash
|
|
git branch -vv
|
|
```
|
|
|
|
## Best practices
|
|
|
|
- Use descriptive branch names
|
|
- Delete branches after merging
|
|
- Keep branch names short but meaningful
|
|
- Use consistent naming conventions (e.g., `feature/`, `bugfix/`)
|
|
- Regularly clean up stale branches
|
|
- Don't work directly on `main` or `master`
|
|
|
|
## Related commands
|
|
|
|
- [git checkout](/git/checkout) - Switch branches
|
|
- [git merge](/git/merge) - Combine branches
|
|
- [git push](/git/push) - Push branch to remote
|
|
- [git fetch](/git/fetch) - Update remote branch info
|
|
- [git rebase](/git/rebase) - Reapply commits
|