Skip to main content

Syntax

gitsw -d <BRANCH>
gitsw --delete <BRANCH>
```text

<ParamField path="BRANCH" type="string" required>
  Name of the branch to delete. Cannot be the current branch.
</ParamField>

## Description

The `--delete` flag safely deletes a local Git branch and automatically cleans up any gitsw-managed stash associated with it. This prevents orphaned stashes from accumulating in your repository.

## Implementation

The `delete_branch` function (src/main.rs:254-301) executes the following steps:

### 1. Validation
- Checks if trying to delete the current branch → Error
- Checks if branch exists → Error if not found

### 2. Stash cleanup check
If the branch has a stash in gitsw state:
- Verifies the stash OID exists in Git's stash list
- Displays warning: `warning: Branch has a stash. It will be dropped.`
- Prompts for confirmation
- Drops the stash from Git if confirmed

### 3. Delete confirmation
If no stash or stash already handled:
- Prompts: `Are you sure you want to delete '{branch}'?`
- User can confirm or abort

### 4. Execute deletion
- Deletes the Git branch using `git branch -d`
- Removes branch from gitsw state (`.git/git-switch.json`)
- Displays: `done: Deleted branch '{branch}'`

### 5. Abort handling
If user declines confirmation:
- Displays: `info: Delete aborted.`
- No changes made

## Example Usage

### Delete branch without stash
```bash
$ gitsw -d old-feature
Are you sure you want to delete 'old-feature'? [y/N]
y
done: Deleted branch 'old-feature'

Delete branch with stash

$ gitsw -d feature-login
warning: Branch has a stash. It will be dropped.
Are you sure you want to delete 'feature-login'? [y/N]
y
done: Deleted branch 'feature-login'
```text

### Abort deletion
```bash
$ gitsw -d feature-api
warning: Branch has a stash. It will be dropped.
Are you sure you want to delete 'feature-api'? [y/N]
n
info: Delete aborted.

Error Messages

Cannot delete current branch
error: Cannot delete the current branch
You cannot delete the branch you’re currently on. Switch to another branch first.
Branch not found
error: Branch '{branch}' not found
The specified branch does not exist in the repository.

Warnings

Stash data will be permanently lostWhen you delete a branch with a stash, the stashed changes are permanently removed from Git. Make sure you don’t need those changes before confirming deletion.
Cannot delete current branchYou must switch to a different branch before deleting. Use:
gitsw main
gitsw -d old-branch

Safety Features

Confirmation prompt

The function always prompts before deletion using prompt::confirm_delete():
  • Shows clear warning if stash exists
  • Requires explicit confirmation
  • Defaults to “No” if user presses Enter

State cleanup

After successful deletion:
  • Branch removed from .git/git-switch.json
  • Stash OID reference removed from state
  • Lock file hash removed from state
  • Recent branches list updated

Git safety

Uses Git’s standard branch deletion, which will fail if:
  • Branch has unmerged changes (use git branch -D to force)
  • Branch is checked out in another worktree

Use Cases

Clean up merged feature branch

# After merging to main
gitsw main
gitsw -d feature-auth
```bash

### Remove abandoned branch with stash
```bash
gitsw -d old-experiment
# Confirms and cleans up any stashed work

Check before deleting

# First check if branch has stash
gitsw -l

# Then delete
gitsw -d branch-name
```bash

## Related Commands

- [list](/reference/list) - Check if branch has a stash before deleting
- [switch](/reference/switch) - Switch away from branch before deleting
- [status](/reference/status) - View current branch (can't delete current)

Build docs developers (and LLMs) love