Skip to content

Instantly share code, notes, and snippets.

@ricog
Last active August 20, 2025 18:08
Show Gist options
  • Save ricog/8eb0a65e108e47bfce61e2c4eeb3c28f to your computer and use it in GitHub Desktop.
Save ricog/8eb0a65e108e47bfce61e2c4eeb3c28f to your computer and use it in GitHub Desktop.
Git 'done' alias for post-merge cleanup

Git "done" Alias

A convenient git alias for cleaning up after merged pull requests.

Installation

git config --global alias.done '!f() { current_branch=$(git rev-parse --abbrev-ref HEAD); if [ "$current_branch" = "main" ]; then echo "Already on main branch"; git fetch && git pull && git remote prune origin; else git fetch && git checkout main && git pull && git branch -d "$current_branch" && git remote prune origin; fi; }; f'

Usage

After your PR is merged, simply run:

git done

What it does

  1. Fetch - Gets latest changes from origin
  2. Checkout main - Switches to main branch (if not already there)
  3. Pull - Updates local main with latest changes
  4. Delete branch - Removes the feature branch you were working on (with -d for safety)
  5. Prune origin - Cleans up stale remote tracking branches

Recommended GitHub Configuration

This alias works best when your GitHub repository is configured to automatically delete merged PR branches:

# Configure via GitHub CLI
gh api repos/OWNER/REPO --method PATCH --field delete_branch_on_merge=true

# Or enable via GitHub web UI:
# Settings → General → Pull Requests → "Automatically delete head branches"

With this setting enabled, the remote branch is deleted when you merge the PR, and git remote prune origin cleans up the local tracking reference.

Best Practices

This alias works particularly well with rebase-based git workflows where:

  • Feature branches are rebased onto main before merging
  • PRs are merged using "Rebase and merge" or "Squash and merge" strategies
  • Local feature branches can be safely deleted after merge since history is preserved in main

Safety Features

  • Uses git branch -d (not -D) to ensure branch is fully merged before deletion
  • Handles case where you're already on main branch
  • Only deletes the current working branch, never main

Perfect for streamlining your git workflow after PRs are merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment