Rebase Branch
Goal
Move a branch onto the newest commits of the mainline, and resolve each conflict with your
own judgement.
Quick start
git status --short --branch
git branch "backup/$(git branch --show-current)/$(date +%Y%m%d-%H%M%S)"
git fetch origin && git rebase origin/<mainline>
- DO stop and tell me when the working tree holds changes of mine; they are mine to
place, unless a rebase is in progress, when they are the conflict itself
- DO rebase onto
origin/<mainline>; git fetch moves that ref, and leaves the local
branch of the same name on the commit it already held
- DO make the backup branch; the verification below compares against it
- DO take the backup from the tip that git saved, when a rebase is already in progress;
HEAD is detached, so
--show-current prints nothing, and ORIG_HEAD can point at another
commit after a reset:
git branch "backup/rebase/$(date +%Y%m%d-%H%M%S)" "$(cat "$(git rev-parse --git-path rebase-merge/orig-head)")"
- DO rebase a branch that sits on another branch with
git rebase --onto origin/<mainline> <that branch>; a plain rebase replays commits the mainline already
holds another way
How to resolve a conflict
git diff --name-only --diff-filter=U
git add <file>
GIT_EDITOR=true git rebase --continue
- DO set
GIT_EDITOR=true; this environment shows no editor, and git waits forever
- DO resolve and
git add every file the first command lists, then continue; git stops
again at the next commit it cannot replay, so repeat the block until it reports success
- DO pick a side for a binary file on purpose, with
git checkout --theirs -- <file> for
my version or --ours for the mainline, before the git add; it holds no marker, and git
leaves the mainline version in the tree for git add to keep
- DO stop and ask me when a resolution is unclear, and leave the rebase in progress;
git rebase --abort loses each resolution that you made, so run it only when I say so
How to verify
git log --format=%s origin/<mainline>..HEAD
git log --format=%s origin/<mainline>..<backup>
git grep -n "^<<<<<<< \|^>>>>>>> " HEAD
git diff origin/<mainline> HEAD
git diff <backup> HEAD
- DO account for each subject that the first two commands do not agree on; the rebase
rewrote every hash, so the subjects are what compare. A subject that only the second prints
is a commit that is gone: the mainline holds its patch already,
--onto left it out, or a
resolution emptied it, and git reports each case as success with an exit status of 0
- DO read the third command; git commits a marker I left behind and reports success
- DO account for each removed line in both diffs: in the first, one of my commits must
remove it on purpose, and in the second, the mainline must be the cause; any other removed
line is lost work
- DO run the build and the tests of the project; a commit that replays with no conflict
can still use a name that the mainline changed. Put the fix in a new commit, and tell me
- DO NOT push, and DO NOT delete the backup branch, until I ask
How to push
Push only when I ask, and only after the verification:
git push --force-with-lease --force-if-includes origin <branch>
- DO use both flags, and never
--force; a rebase rewrites each commit, so a plain push is
rejected. The lease refuses when the branch on origin moved after the fetch, and
--force-if-includes refuses when the fetch brought a commit that this branch never held
- DO stop and tell me when git rejects the push; the branch on
origin holds a commit of
another session, and a forced push removes it
Reference
A rebase replays my commits onto the mainline, so the sides are the opposite of a merge:
| Side |
Marker |
Content |
--ours |
<<<<<<< HEAD |
The mainline, and each commit of mine already replayed |
--theirs |
>>>>>>> <commit> |
The commit of mine being replayed |
Copyright © 2026 Lorenzo Mazzarotto
1---2name: rebase-branch3description: Rebases a branch onto another branch and resolves each conflict. Use when rebasing onto a mainline, continuing a rebase that is already in progress, resolving rebase conflicts, or when a pull request conflicts with its base branch.4metadata:5 source: https://skillmd.com/skills/mazzsva/rebase-branch6---78# Rebase Branch910## Goal1112Move a branch onto the newest commits of the mainline, and resolve each conflict with your13own judgement.1415## Quick start1617```bash18git status --short --branch19git branch "backup/$(git branch --show-current)/$(date +%Y%m%d-%H%M%S)"20git fetch origin && git rebase origin/<mainline>21```2223* **DO** stop and tell me when the working tree holds changes of mine; they are mine to24 place, unless a rebase is in progress, when they are the conflict itself25* **DO** rebase onto `origin/<mainline>`; `git fetch` moves that ref, and leaves the local26 branch of the same name on the commit it already held27* **DO** make the backup branch; the verification below compares against it28* **DO** take the backup from the tip that git saved, when a rebase is already in progress;29 HEAD is detached, so `--show-current` prints nothing, and `ORIG_HEAD` can point at another30 commit after a reset:31 `git branch "backup/rebase/$(date +%Y%m%d-%H%M%S)" "$(cat "$(git rev-parse --git-path rebase-merge/orig-head)")"`32* **DO** rebase a branch that sits on another branch with `git rebase --onto33 origin/<mainline> <that branch>`; a plain rebase replays commits the mainline already34 holds another way3536## How to resolve a conflict3738```bash39git diff --name-only --diff-filter=U40git add <file>41GIT_EDITOR=true git rebase --continue42```4344* **DO** set `GIT_EDITOR=true`; this environment shows no editor, and git waits forever45* **DO** resolve and `git add` every file the first command lists, then continue; git stops46 again at the next commit it cannot replay, so repeat the block until it reports success47* **DO** pick a side for a binary file on purpose, with `git checkout --theirs -- <file>` for48 my version or `--ours` for the mainline, before the `git add`; it holds no marker, and git49 leaves the mainline version in the tree for `git add` to keep50* **DO** stop and ask me when a resolution is unclear, and leave the rebase in progress;51 `git rebase --abort` loses each resolution that you made, so run it only when I say so5253## How to verify5455```bash56git log --format=%s origin/<mainline>..HEAD57git log --format=%s origin/<mainline>..<backup>58git grep -n "^<<<<<<< \|^>>>>>>> " HEAD59git diff origin/<mainline> HEAD60git diff <backup> HEAD61```6263* **DO** account for each subject that the first two commands do not agree on; the rebase64 rewrote every hash, so the subjects are what compare. A subject that only the second prints65 is a commit that is gone: the mainline holds its patch already, `--onto` left it out, or a66 resolution emptied it, and git reports each case as success with an exit status of 067* **DO** read the third command; git commits a marker I left behind and reports success68* **DO** account for each removed line in both diffs: in the first, one of my commits must69 remove it on purpose, and in the second, the mainline must be the cause; any other removed70 line is lost work71* **DO** run the build and the tests of the project; a commit that replays with no conflict72 can still use a name that the mainline changed. Put the fix in a new commit, and tell me73* **DO NOT** push, and **DO NOT** delete the backup branch, until I ask7475## How to push7677Push only when I ask, and only after the verification:7879```bash80git push --force-with-lease --force-if-includes origin <branch>81```8283* **DO** use both flags, and never `--force`; a rebase rewrites each commit, so a plain push is84 rejected. The lease refuses when the branch on `origin` moved after the fetch, and85 `--force-if-includes` refuses when the fetch brought a commit that this branch never held86* **DO** stop and tell me when git rejects the push; the branch on `origin` holds a commit of87 another session, and a forced push removes it8889## Reference9091A rebase replays my commits onto the mainline, so the sides are the opposite of a merge:9293| Side | Marker | Content |94| --- | --- | --- |95| `--ours` | `<<<<<<< HEAD` | The mainline, and each commit of mine already replayed |96| `--theirs` | `>>>>>>> <commit>` | The commit of mine being replayed |9798Copyright © 2026 Lorenzo Mazzarotto