Resolve Conflicts
Rebase the current branch onto the target branch to resolve PR conflicts.
Rebase the current branch onto the target branch to resolve PR conflicts.
Steps:
- Determine the target branch using the first method that succeeds:
- Run
gh pr view --json baseRefName,url 2>/dev/nullto get the PR's target branch and URL (requiresghand GitHub). - Run
git remote show origin 2>/dev/null | grep 'HEAD branch'to detect the default branch from the remote config (works with any git host). - Check which of
main,master, ordevelopexist on the remote withgit branch -r. - If none of the above work, ask the user to specify the target branch and stop until they do.
- Run
- Run
git fetch originto ensure the latest remote changes are available. - Run
git rebase origin/<target-branch>. - If the rebase succeeds with no conflicts, confirm to the user and skip to step 8.
- If the rebase stops with conflicts:
- Run
git diff --name-only --diff-filter=Uto list conflicting files. - For each conflicting file, read the file and show the conflict markers clearly.
- Resolve each conflict by choosing the correct code based on context:
- Prefer the incoming change (
theirs) if it supersedes the current change. - Prefer the current change (
ours) if the incoming change is unrelated. - Merge both sides if both changes are needed.
- Prefer the incoming change (
- After editing each file, run
git add <file>to mark it resolved.
- Run
- Once all conflicts are resolved, run
git rebase --continueto proceed. If more conflicts arise, repeat step 5. - If at any point a conflict is too ambiguous to resolve safely, run
git rebase --abort, explain what was ambiguous, and ask the user to resolve it manually. - Run
git push --force-with-leaseto update the remote branch. - Report the result: confirm the branch is rebased and print the PR URL if one was found in step 1, otherwise just confirm the branch is up to date with the target.
$ARGUMENTS