Skip to content

commit-push-pr fails when remote branch has diverged (draft PR exists) #195

@shanev

Description

@shanev

Problem

When using /commit-push-pr on a branch that already has a draft PR, the command fails with:

git push -u origin <branch> failed: error: failed to push some refs to 'github.com:...'

This happens because the remote branch may have diverged since the last push (e.g., rebased via GitHub UI, updated from another machine, or modified by CI).

Current Behavior

The command does a simple git push which fails if the remote branch is ahead or has diverged.

Expected Behavior

The command should:

  1. Detect if the remote branch exists and has diverged
  2. Either pull/rebase automatically, or prompt the user for how to proceed
  3. Skip gh pr create if a PR already exists for the branch

Workaround

When this happens, users can manually run:

git pull --rebase origin <branch-name>
git push

Or to overwrite the remote:

git push --force-with-lease

Suggested Fix

Before pushing, check if the remote branch exists and compare commits:

git fetch origin
if git rev-parse --verify origin/<branch> >/dev/null 2>&1; then
  # Remote exists, check if we need to rebase
  if ! git merge-base --is-ancestor origin/<branch> HEAD; then
    git pull --rebase origin <branch>
  fi
fi
git push -u origin <branch>

Also check for existing PR before creating:

if ! gh pr view --json number >/dev/null 2>&1; then
  gh pr create ...
fi

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions