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
- Fetches the latest from remote
- Rebases the branch onto the new target using
git rebase --onto origin/<new-target> <old-parent-head> <branch> - Pushes with
--force-with-lease - Updates the stack config to record the new parent
- Retargets the PR via
gh pr edit <branch> --base <new-target>(ifghis available)
Examples
Auto PR Retarget
If the GitHub CLI (gh) is installed and authenticated, gx automatically runs:
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:Details
Details
- 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_headSHA as the old base for--onto, which is more reliable than using the branch name when branches have been rebased