Skip to main content
gx retarget moves a branch from one parent to another using git rebase --onto, updates the stack config, pushes the result, and optionally retargets the GitHub PR.

Usage

Arguments

If only one argument is provided, it is treated as the new target and the current branch is retarget.

Flags

What Happens

  1. Fetches the latest from remote
  2. Rebases the branch onto the new target using git rebase --onto origin/<new-target> <old-parent-head> <branch>
  3. Pushes with --force-with-lease
  4. Updates the stack config to record the new parent
  5. Retargets the PR via gh pr edit <branch> --base <new-target> (if gh is available)

Examples

Auto PR Retarget

If the GitHub CLI (gh) is installed and authenticated, gx automatically runs:
If this succeeds, you see OK PR for <branch> automatically retargeted to <new-target>. If it fails (no PR exists, or gh is not available), gx prints a reminder: WARN Remember to retarget the PR for <branch> to <new-target>.

Safety Guards

gx retarget blocks if the working tree is dirty (uncommitted changes). You must commit or stash your changes before retargeting:

Conflict Handling

If a rebase conflict occurs during retargeting:
  • If the branch is already based on the new target, gx prints > <branch> is already based on <target>. Nothing to do.
  • If no saved parent exists in the stack config, gx falls back to using the merge base and prints a warning
  • The rebase uses origin/<new-target> when the remote branch exists, ensuring you rebase onto the latest remote state
  • Uses the stored parent_head SHA as the old base for --onto, which is more reliable than using the branch name when branches have been rebased