Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

gg clean

Delete merged stacks (and associated managed worktrees).

gg clean [OPTIONS]

Options

  • -a, --all: Clean all merged stacks without prompting
  • --json: Emit machine-readable JSON output

Worktrees and incomplete cleanup

Clean never removes the worktree you run it from. If your current branch is one of the stack’s branches, Clean first switches to the stack’s base branch, or detaches HEAD when the base is checked out elsewhere, and then deletes the branch.

A stack’s managed worktree is removed only after you confirm the prompt. The same goes for any other linked worktree that has one of the stack’s branches checked out. With --json, or without an interactive terminal, Clean never removes those worktrees. It skips the stack instead and reports why. Declining also skips the stack, because its branches cannot be deleted while a worktree holds them.

Deleting the stack’s local branches is required, not best-effort. If any branch cannot be deleted, Clean reports the failure, exits with an error, and keeps the stack’s configuration so you can fix the cause and run gg clean again.

Queued remote deletions after gg land

gg land merges an entry and removes its PR/MR mapping, so the stack no longer remembers which remote branch belonged to it. To keep that branch deletable, land queues a deletion for it at the commit that was merged, in the same config write that removes the mapping. This happens with or without --clean, and it works when no local entry branch exists.

Every gg clean (with or without --all) first drains the queued deletions for your entry branches, from any stack, even one whose configuration is already gone. gg land --clean does the same when the whole stack lands and cleanup runs. Each branch ends in one of three ways:

  • deleted: the branch was still at the merged commit and no open review targets it, so it was deleted from origin.
  • deferred: an open review targets it, there is no provider, or the remote could not be read. Clean prints a warning, keeps the queued deletion and exits 0. Retarget or close the review, then run gg clean again.
  • blocked: the branch moved since it was merged, the remote refused the delete (a server hook or protected branch; the deletion stays queued), the delete push failed and the branch is still at the merged commit (nothing changed; the deletion stays queued and the next gg clean retries it), or an earlier delete with an unknown outcome needs a decision. Clean prints one message for that branch and carries on with the others, then exits non-zero. Delete the branch yourself if the new commits are not needed, or keep it with gg reconcile --discard-pending.

gg land --clean keeps its exit code in these cases, but prints a warning and reports cleaned: false. If gg clean verifies a stack is merged but has to skip or defer its remote delete, it queues the deletion before dropping the stack configuration, so a later gg clean still finishes it. gg sync never runs or drops these deletions.

For example, an outside review still targets a landed entry:

$ gg land --all --clean
! Kept remote branch 'alice/feat--c-abc1234': open PR #103 still targets it. Retarget or close it, then run `gg clean` again; or keep the branch with `gg reconcile --discard-pending`.
$ gg clean --all     # after closing #103

Branches that older versions (v0.10.10 and earlier) left behind have no queued deletion. After confirming the review is merged, delete them by hand:

git push origin --delete <user>/<stack>--<gg-id>

Examples

gg clean
gg clean --all
gg clean --json

--json prints:

  • version: output schema version
  • clean.cleaned: stacks that were cleaned
  • clean.skipped: stacks skipped (unmerged or declined in interactive mode)
  • clean.remote_deletions: only when queued deletions were drained or kept: {"deleted": ["<branch>"], "deferred": [{"branch": "...", "reason": "..."}], "blocked": [{"branch": "...", "reason": "..."}]}. Omitted when all three lists are empty.

When merge verification allows Clean to delete an entry branch from origin, the Clean operation records a branch_deleted remote effect with the branch’s exact server-side OID. Deletion uses a matching force-with-lease so a concurrent server update is never deleted or misreported. Before deleting, Clean asks the provider for open PRs/MRs whose target is that branch, both before saving the deletion and again immediately before the push. That applies to gg clean and to gg land --clean. If a review still targets the branch, or the lookup is impossible because no provider is detected, the request fails, or the output is malformed, Clean keeps the remote branch, the local entry branch, and the stack configuration. It reports the blocking review and leaves the deletion retryable. Retarget the review, for example with a full gg sync, then run gg clean again. Each remote branch deletion has its own durable checkpoint in the clean’s operation record: uncertainty is saved before the request, and the exact effect is saved and checkpointed before Clean reports success or starts the next deletion. The whole gg clean is one record, so once it deleted a remote branch none of it is locally undoable. Clean also saves the branch and expected OID as a remote deletion entry in the config’s pending_remote_changes journal before a direct request. If the request outcome is ambiguous, Clean leaves that entry attempted and stops. The next gg clean checks the remote first. If the branch is gone, the deletion took effect and the entry is retired. If it is still at the expected commit, the deletion is retried: Clean sends the leased push again through the same open-review check. If it now points at another commit, Clean keeps that stack, reports it as skipped with an error naming gg reconcile, and still cleans the other stacks. Managed worktree removal clears and durably saves its configured path, then finalizes its local checkpoint before printing the unchanged Removed worktree success message. A retry reconciles an already removed, unregistered worktree after a failed config save. gg undo refuses local replay for a completed remote deletion and prints the exact git push origin <prior_oid>:refs/heads/<branch> recovery command.