Skip to navigation

Claude Code Git Rebase: Choose a Branch Policy

In short: Match merge or rebase to repository rules, protect shared history, and verify the resulting diff before pushing.

Published 19 min read
On this page

Direct answer

Decide merge versus rebase from the repository’s written policy, then verify the resulting diff before it reaches a shared branch. Rebase replays your commits on top of an upstream branch, which rewrites their identities. That is safe for private work and unsafe for commits other engineers already depend on. Claude Code can run either operation, but the choice belongs to the branch contract, not to personal taste.

A practical rule covers most cases. Read the branch protection settings first. If they require a linear history, plan for rebase or squash on private branches. If they allow merge commits, use merge for shared integration work. After either operation, compare the diff against what you expect and rerun the relevant checks. The policy is the authority; the tool executes it.

Evidence map linking branch policy, replay behavior, shared history, and resulting diff for a rebase decision.
Editorial evidence map based on F055, F056, and F057. It organizes the decision inputs; it is not a measured reliability result for rebase operations.

Why an unclear policy costs a whole afternoon

An undocumented branch policy turns every pull request into a negotiation. One engineer rebases, another expects merge commits, and the reviewer spends the review explaining history instead of reading code. The cost is not the command. The cost is the rework after someone force-pushes over a teammate’s base.

Claude Code raises the stakes slightly because it moves fast. The agent can produce a clean-looking local replay in seconds. If the branch was shared, that clean local state becomes a broken remote state the moment it is published. Reviewers then see duplicated commits, lost review comments, and a history that no longer matches the discussion.

The problem also shows up in the opposite direction. A team that forbids force-pushes but allows merges may receive a long chain of “fix typo” commits. Squash merging at the end can clean the final history, but only when the repository allows it. Without a written decision, each contributor guesses, and the guesses disagree.

Read the repository branch policy

The first step is reading, not running. Open the branch protection settings for the target branch and check three things: whether merge commits are allowed, whether squash merging is allowed, and whether a linear history is required.

Official, F056: GitHub documents branch rules that can require a linear commit history. The exact rules available depend on repository ownership, plan, and settings, so a rule that exists in one repository may be absent in another.

Record what you find before touching history. A short note in the pull request description is enough: “target branch requires linear history” or “target branch allows merge commits.” That note tells reviewers which operation to expect and gives the next engineer a reference.

Repositories without protection rules need a written convention somewhere durable. A CONTRIBUTING file, a team wiki page, or a pinned message all work. An undocumented preference in someone’s head is not a policy.

Branch Owner Shared status Allowed integration method
main Release team Shared Merge commit or squash, per protection rule
release/2026.4 Release team Shared, read-mostly Merge commit only, no history rewrite
feature/payment-retry You Private until review Rebase or squash allowed
feature/shared-utils Two engineers Shared among authors Merge only until the branch closes
dependabot/npm-bump Automation Private Rebase or squash allowed

The shared column matters more than the branch name. A “feature” branch with two active authors is shared work, and a long-lived release branch is shared even when most people only read it.

Workflow from reading branch policy to identifying shared commits, replaying private work, and comparing the resulting diff.
Editorial workflow based on F055 and F057. The sequence describes a review discipline, not an automated Claude Code procedure.

Identify shared and private commits

A commit is shared when someone else has built on it, reviewed it, or pushed it to a branch other people pull. A commit is private when it exists only in your local repository or in a branch you own alone.

The distinction is not about time. A commit pushed an hour ago to a branch with two other contributors is shared. A commit that sat locally for a week on a branch nobody else checks out is still private.

Ask Claude Code to help with the inventory. A prompt like “list the commits on this branch that are not on main, and tell me which ones appear in any other remote branch” produces a starting list. Verify it with a commit graph view or a comparison view in the hosting UI. The agent reads history; it does not know who cloned the branch last night.

Commit range Replay risk Conflict likelihood Verification step
Local commits never pushed Low Depends on upstream drift Diff before and after replay
Commits pushed to a private branch Low to medium Low Diff plus focused tests
Commits pushed to a branch with co-authors High Medium to high Confirm with co-authors before replay
Commits already merged to a protected branch Not replayable safely Not applicable Use a forward fix instead

The last row is the hard boundary. Commits that already landed on a protected branch are part of shared history. The safe correction is a new commit that fixes the problem, not a rewrite that pretends the old one never happened.

Understand what rebase actually replays

Official, F055: Git documents rebase as replaying commits from the working branch on top of an upstream branch. The documentation also notes that replaying can produce conflicts and new commit identities.

Two consequences follow from that wording. First, a replay is not a copy. Each replayed commit gets a new identity, so the commit you pushed and the commit that exists after the replay are different objects with the same message. Second, conflicts can appear during replay even when the final tree looks similar, because each commit is applied one at a time against a moving base.

A clean replay also does not prove the behavior survived. It proves the text applied without a conflict. Semantic interactions between the replayed change and the new upstream code can still change what the program does. That gap closes only with tests.

Claude Code can explain the range it plans to replay before starting. A useful check is to ask for the commit list and the target upstream, then confirm that the range excludes anything already published to a shared branch.

Choose merge or rebase deliberately

The decision matrix below keeps the reasoning explicit. It assumes the branch policy is known; where it is not, the first row applies.

Situation Recommended method Reason Risk to manage
Policy requires linear history Rebase or squash Policy forbids merge commits Rewrite only private commits
Policy allows merge commits, shared branch Merge Preserves published history Extra merge commit in history
Long-running feature with noisy WIP commits Rebase, then squash where allowed Reviewers read fewer commits Squash can hide intermediate fixes
Hotfix against a release branch Merge Release branches are read-mostly and shared Verify the fix on the release line
Automated dependency bump on a private branch Rebase or squash Low review value per commit Confirm CI covers the replayed result

Official, F056: GitHub documents configurable squash merging for pull requests, which lets the hosting service collapse a branch’s commits into one at merge time. Squash changes the final published history regardless of how tidy the local branch was.

The practical consequence is that local atomic commits still earn their keep even when squash is enabled, because they are what you reason about during review. Organizing a change into revertible units covers the local structure in detail.

Do not present rebase as universally safer than merge. A merge commit records that two lines of work converged. A replay produces a straight line. Which one the repository wants is a policy question.

Resolve conflicts without hiding semantics

Rebase stops at the first conflicting commit and asks for a resolution. The temptation at that prompt is to accept whichever side compiles. That habit is how a behavior change disappears during integration.

Treat each conflicted hunk as a decision about behavior, not about text. Read what the incoming side intended, read what the local side intended, and write down the combined behavior you are about to commit. Resolving conflicts by preserving both behaviors gives that method a full treatment.

A focused pass is faster than a broad one. Ask Claude Code to explain the two sides of a hunk in plain language, then make the semantic choice yourself. The agent is useful for describing intent; the reviewer owns the decision about which behavior the product should have.

Deleted lines deserve extra suspicion. A conflict that removes a guard clause or a test may be resolving the marker by discarding someone’s protection. Before completing the resolution, check whether any test or assertion disappeared.

Resolution choice Alternative considered Test to run Reviewer
Keep both validations Drop the older one Validation suite for the touched field Backend owner
Keep the new guard and the old fallback Keep only the new guard Error-path test for the old fallback Original guard author
Re-apply a removed test Skip the test Run the test in isolation QA contact
Take upstream version of a config Keep local override Configuration smoke check Platform owner

Compare the resulting diff

The diff is the evidence that the replay did what you expected. Git show can display any single commit’s change, and a range comparison can show the branch’s total effect against the target.

Run two comparisons. First, compare the tip of the replayed branch against the tip of the original branch before the operation. The tree should be identical when the replay is behavior-preserving. Second, compare the replayed branch against the upstream target to see the change a reviewer will read.

Comparison Before After Test result Policy decision
Local tree vs pre-replay tree Baseline Expected identical Clean Replay acceptable
Branch vs target, file list 6 files 6 files Clean No scope drift
Branch vs target, added lines 142 142 Clean Content preserved
Focused suite for touched module Passing Passing Green Proceed to push
Full required checks on the PR Not yet run Pending Pending Wait for CI

A tree that changed unexpectedly means the replay did something more than replay. Stop and inspect the individual commits before pushing anything. Ask Claude Code to list which commits changed which files, then compare that list with the plan.

The diff comparison also catches a common rebase mistake: replaying a commit that was already merged. If the after-diff contains changes you thought were upstream, the range probably included published work.

Verify tests after history changes

History changes do not change the code’s meaning by themselves, but they can expose interactions that the original base hid. The verification standard is the same one the repository applies to any pull request.

Start with the checks closest to the change. A touched module’s unit tests, a related integration test, and any contract test that covers the modified interface are the first line. Broaden only when those pass and uncertainty remains.

Official, F055: Because a replay can produce conflicts and new commit identities, the commit that existed before the replay is not the commit that exists after. CI therefore has to run against the new tip, not against a cached result from the old one.

Treat a green CI run on a stale commit as no evidence at all. If the replay rewrote the branch tip, the required checks need to finish on the new tip before the pull request is mergeable.

The guide to false success from passing tests explains the general trap: a suite can be green and still miss the changed behavior. After a replay, the added risk is that the suite ran against a different tree than the one being merged.

Document the chosen integration method

A pull request that says how it will land saves the reviewer a round trip. One sentence in the description is enough: “squash merge, history already linear” or “merge commit, branch was shared with two authors.”

The note becomes more valuable over time. When a future engineer wonders why a merge commit exists on a branch that usually rebases, the pull request explains the exception. Without the note, the history looks inconsistent and someone may “fix” it.

Review gate Pass condition Failure response
Policy recorded The target branch’s rule is stated in the PR Read the protection settings and add the note
Shared commits identified No published commit is in the replay range Remove published commits from the range
Diff comparison Tree matches the pre-operation baseline Stop and inspect the replay
Conflicts resolved by behavior Each hunk has a recorded decision Revisit hunks resolved by “take theirs”
Checks on the new tip Required checks ran after the replay Wait for or retrigger the checks
Method documented The PR says which integration method is used Add one sentence to the description
Tradeoff between a linear replayed history and the risk of rewriting commits other engineers depend on.
Editorial tradeoff diagram based on F055 and F056. A linear history is easier to read but rewrites identities, which is only safe for private work.

Worked example: a feature branch against a protected main

Suppose a branch named feature/retry-backoff has six commits, all local, and main has moved forward by nine commits since the branch started. The repository requires a linear history on main and allows squash merges.

The branch policy is known: linear history required. The six commits are private because the branch has not been pushed and has no co-authors. Rebase is the appropriate method.

After the replay, two comparisons follow. The tree of feature/retry-backoff should match the tree before the replay. The diff between feature/retry-backoff and main should show only the six commits’ changes, no more.

Suppose one commit conflicts because main also changed the retry configuration. The resolution records a decision: keep the new upstream configuration key and the local backoff calculation. The focused test for the retry path runs after the resolution and passes.

Derived arithmetic for the review scope: The branch contained 6 commits. The replay produced 6 commits with new identities on a new base. Reviewer-visible commits added by the replay: 6 - 6 = 0 beyond the original intent, and the diff against main covers the same 6 logical changes. This arithmetic checks the replay’s scope; it is not a benchmark result.

The concrete win is a pull request whose history matches the policy and whose diff is exactly the intended change. The concrete loss is that the replay rewrote six commit identities, so any local bookmark or review comment tied to the old identities no longer points at the current commits.

Practical checklist

  1. Read the protection settings for the target branch before planning the operation.
  2. Record the policy in the pull request description or the team’s contribution guide.
  3. Classify each commit as shared or private based on who has it.
  4. Replay only private commits. For shared work, use a merge.
  5. Resolve each conflict by behavior, writing down the combined intent.
  6. Check for deleted guards and tests in every conflicted hunk.
  7. Compare the tree before and after the operation.
  8. Rerun focused checks and wait for required checks on the new tip.
  9. Document the integration method in the pull request.
  10. Keep force-push decisions behind a human checkpoint rather than making them the default path.
Control checklist covering policy read, shared commit classification, diff comparison, and post-replay verification.
Editorial control checklist derived from F055 through F057. Passing the checklist reduces replay risk; it does not remove the need for human review of the resulting diff.

Concrete win, honest loss, and practical implication

Win: The branch lands with the history shape the repository requires, the diff a reviewer reads is the change that was intended, and no published commit was rewritten. That combination removes the most common source of rework in integration review.

Loss: Rebase still rewrites commit identities even when everything else goes right. Review comments anchored to old commit hashes can dangle, and any tooling that tracked the branch by commit identity needs to be pointed at the new ones. The method does not make the rewrite free.

Implication: Make the branch policy visible and let it decide. For a private branch that has not been pushed, a replay is cheap and keeps review readable. For shared work, a merge preserves the history other people already rely on. Either way, the diff comparison and a focused test run are what turn a clean-looking replay into evidence.

Verdict that branch policy should decide merge versus rebase, with diff comparison and tests as the verification gate.
Editorial verdict based on F055 through F057. The benefit is an integration method that matches the repository contract; the residual risk is that a clean replay can still change behavior without tests.

Where this connects in the batch

Conflict resolution during a replay is the same discipline as resolving merge conflicts by behavior. The two posts share the rule that a clean compile is not evidence of preserved intent.

The commit boundaries you keep locally feed directly into making changes revertible as atomic commits. Commit messages written with intent in mind pair with encoding change intent in commit messages.

When the branch reaches the queue, recovering failed merge-queue groups covers what happens after the replay, and defining merge checks with branch protection covers the settings that produced the policy in the first place.

For adjacent hazards, the destructive git-reset guardrails post covers the recovery command people reach for when a replay goes wrong, and the rewind checkpoint limits post explains what session-level undo does and does not restore.

The six posts in this batch form one sequence. Resolving merge conflicts by behavior covers the decisions a replay or merge asks for. Making changes revertible with atomic commits covers the unit structure that makes a branch readable. Encoding change intent in commit messages covers the record each unit leaves. Recovering failed merge-queue groups covers what happens after the branch is pushed. Defining merge checks with branch protection covers the settings that produced the policy this post starts from.

Release-facing decisions continue in choosing a version impact with SemVer, annotated release tags, hotfix backports and cherry-picks, planning a rollback with revert, preparing a changelog from consumer-visible changes, and running a release checklist. Testing and debugging coverage starts at selecting tests for changed code and triaging flaky tests.

Adjacent library posts that share this post’s safety concerns include destructive git-reset guardrails, rewind checkpoint limits, parallel agents with worktrees, the code review bottleneck, overengineering large diffs, false success from passing tests, verification done proof, and session handoff for team context. For the platform side of a push, GitHub Actions secrets covers what continuous integration can read, and repository hooks security covers the hooks that run on the way in. Estimating monthly LLM API cost is the non-blog companion for the spend side of long integration sessions.

Data gap and future update

The fact pack contains no measured comparison of merge versus rebase outcomes, no benchmark for review time under either policy, and no reproducible community run measuring conflict rates during replay. No such number is claimed here.

The available official evidence covers what rebase does mechanically and what GitHub branch rules can require. It does not cover how often a linear-history rule changes defect rates. A future update should add a documented repository case study if one becomes publicly available.

Recheck the cited Git and GitHub pages before relying on specific rule names, because hosting settings change across plans. Until stronger evidence exists, treat the policy-first workflow as a reviewable discipline rather than a proven intervention.

One AI API Key option for OpenAI-format agents

Claude Code uses an Anthropic-format route, so the One AI free model IDs are not a Claude Code model source. The research packet records Claude IDs as unavailable on 2026-11-27.

For a separate OpenAI-format agent, One AI API Key offers https://api.oneaiapikey.com/v1, one key, and 46 live free IDs. That option concerns a different client and request format, not a workaround for Claude Code authentication. Review the free model list and eligibility details before choosing an agent route, or start from the quickstart.

FAQ

Does a clean rebase prove the behavior did not change?

No. F055 describes rebase as replaying commits, which can produce conflicts and new commit identities. A replay without conflicts shows that the text applied. Behavior needs tests.

Can Claude Code decide the branch policy for a repository?

It can read the settings and propose a method, but the policy is a repository decision. The engineer approves the method, especially when the operation would rewrite published commits.

Is force-push part of the normal workflow here?

No. Force-push is sometimes required after a legitimate replay of private work, but it is not the default path this post recommends. Confirm the branch is private and the policy allows it first.

What if the repository has no written policy?

Write one before the next pull request, even a two-line version. State which integration method the target branch expects and whether history rewrites are permitted on feature branches.

Why compare the diff after a replay?

The comparison is the check that the replay preserved the intended change and did not accidentally include published commits. A tree that differs from the baseline means the operation did something beyond replaying.

More Claude Code troubleshooting

Sources

  • Official: Git rebase, documenting replay behavior and new commit identities (F055).
  • Official: Git merge, documenting merge as the alternative integration path.
  • Official: GitHub branch protection rules, documenting rules that can require a linear history (F056).
  • Official: GitHub commit squashing, documenting configurable squash merging.
  • Official: Git show, supporting commit-level diff inspection.
  • Official: Semantic Versioning 2.0.0, referenced for release-facing change classification.
  • Derived: F057 supports choosing the method from policy, then comparing the diff and rerunning checks.
More in Blog

Try it

One API key for GPT, Grok, DeepSeek, Kimi, GLM, MiniMax, Qwen, Step, Beam, Nemotron, ERNIE, Hunyuan, Gemma, Laguna, LFM, LongCat, Llama, Solar, Jais, Mistral, K2 and Roleplay.

One AI API Key speaks the OpenAI and Anthropic formats, so you change the base URL and the key, not your code. Credit packs start at $10, and every model has a published per-token rate.

New accounts$10 credit on sign-up
  • 100requests
    per day
  • 3Mtokens
    per day
  • 47free
    models
  • $0per
    token
  • No card
  • No end date
Claim $10 credit →