Git rebase

From phatblat/dotfiles · View interactive version

Git rebase is an AI agent skill from the GitHub repository phatblat/dotfiles. Rebase the current branch onto a target branch using safety checks and an appropriate rebase strategy. Use when invoked as `$git-rebase` or when the user asks to update/rebase a branch.

How to use this skill

Skill file

git-rebase

Rebase the current branch onto a target branch. If the user provides an argument, use it as the target branch; otherwise use the remote default branch.

Workflow

  1. Gather context:

```bash

subject=$(git branch --show-current)

target=<argument-or-default-branch>

git status --porcelain=v1

```

  1. Stop if:
  1. Fetch remote-tracking refs without touching the local target branch:

```bash

git fetch origin --prune

target_ref="origin/${target}"

```

Never use git fetch origin "${target}:${target}" for a branch that may be checked out in another worktree. If a local target branch must be updated, do that only in the worktree that currently has it checked out.

  1. Analyze divergence:

```bash

merge_base=$(git merge-base HEAD "${target_ref}")

git log --oneline "${merge_base}..HEAD"

git log --oneline "${merge_base}..${target_ref}"

git diff --name-only "${merge_base}" HEAD

git diff --name-only "${merge_base}" "${target_ref}"

git merge-base --is-ancestor "${merge_base}" "${target_ref}" && echo base_on_target=yes || echo base_on_target=no

```

  1. Choose strategy:

| Condition | Strategy |

| --- | --- |

| Merge-base is not on target lineage | rebase --onto |

| Otherwise | simple rebase |

Show the analysis and selected strategy before proceeding.

  1. Execute:

Both rebase subject in place. Never create, switch, rename, or delete a branch. Branch switching and git reset --hard may be blocked in a main checkout, so avoid them. Simple rebase also skips commits whose patch is already in the target (rebase-merged or cherry-picked commits that got new hashes). Squash-merged commits are not skipped; they conflict or go empty and are handled in step 7.

  1. On conflicts, follow the git-resolve-conflicts skill (~/.agents/skills/git-resolve-conflicts/SKILL.md). It resolves each path, continues the rebase (skipping commits already upstream), and repeats until the operation completes, pausing only where it says to ask the user. Then continue with step 8.
  1. After success, set tracking and push:

```bash

git branch --set-upstream-to="origin/${subject}" "${subject}"

git push --force-with-lease -u origin "${subject}:${subject}"

git branch -vv

```

  1. Report strategy, branch, and tracking.
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.